View Full Version : Help with a Java program

November 25th, 2003, 09:34 AM
Hi Guys, I am having hard time passing data from one class to another in a swing GUI application in Java.

Would someone be kind enough to write me a simple little two class GUI application (or find me an example from the net) which does the following? I tried searching for it at various places including java.sun but couldn't find anything to help me. :(

Have a two class program. One class is where the GUI is defined with three text boxes and a button. User types numbers in the textBox1 and textBox2. When the button is pressed, #s from text boxes are passed to the Calculator class to add the numbers, returns the total back to the GUI class, and displayed in the third text box.

Thanks a lot! :)

November 25th, 2003, 11:23 AM
Here is the code

The first class is the GUI class to setup a Frame (application)
The second class does the actual number mathematics.

The code is simple and written quickly to help you out. Please tweak it further.

public class MyCalculator extends java.awt.Frame

/** Creates new form MyCalculator */
public MyCalculator() {

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
private void initComponents() {
textField1 = new java.awt.TextField();
textField2 = new java.awt.TextField();
button1 = new java.awt.Button();
textField3 = new java.awt.TextField();


addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {

textField1.setBounds(40, 40, 230, 30);

textField2.setBounds(40, 100, 230, 30);

button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

button1.setBounds(300, 70, 37, 24);

textField3.setBounds(40, 150, 230, 30);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-300)/2, 400, 300);

private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
//Get value of first text Field
String strField1 = textField1.getText().trim();
String strField2 = textField2.getText().trim();
System.out.println("First text field = " + strField1);
System.out.println("Second text field = " + strField2);

//Now convert Strings to integers.
int n_First = 0;
int n_Second = 0;
//ensure only numbers are entered by catching exception
n_First = Integer.parseInt(strField1);

catch(NumberFormatException e)
//ensure only numbers are entered by catching exception
n_Second = Integer.parseInt(strField2);
catch(NumberFormatException e)

//now create a Calculator class
MyNumberCruncher instance_MyNumberCruncher = new MyNumberCruncher();
int result = instance_MyNumberCruncher.getAdditionResults(n_First, n_Second);

//add a blank string character to an integer for quick and dirty conversion to
String strResult = "" + result;
System.out.println("Result = " + strResult);

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {

* @param args the command line arguments
public static void main(String args[]) {
new MyCalculator().show();

// Variables declaration - do not modify
private java.awt.Button button1;
private java.awt.TextField textField1;
private java.awt.TextField textField2;
private java.awt.TextField textField3;
// End of variables declaration


* MyNumberCruncher.java
* Created on November 25, 2003, 11:13 AM

public class MyNumberCruncher {

/** Creates a new instance of MyNumberCruncher */
public MyNumberCruncher()

public int getAdditionResults(int firstNumber, int secondNumber)
//simply add the two numbers and return
int result = firstNumber + secondNumber;

return result;


November 25th, 2003, 11:32 AM
Awesome, thanks a lot for helping. Just what I wanted.

November 26th, 2003, 04:31 PM
I used the free www.netbeans.org IDE to make this program. We used to do our project using Visual Cafe but Visual Cafe debugger dies when code size exceeds a few hundred thousand lines.

netbeans.org works great but since its written in Java, it can be very slow at times.

November 30th, 2003, 02:56 PM
Sorry, I didn't see this post until now since Search feature is disabled (I usually click on the New Posts whenever I come on).

It worked great. Only thing different was that I was working with swing, but functionality was there. I was trying too hard while actual code was straight forward and easy to understand.

We're required to use Boreland's JBuilder in our class.

And thanks again for the code.