PDA

View Full Version : Help with a Java program


Charchila
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! :)

echarcha
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() {
initComponents();
}

/** 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();

setLayout(null);

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

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

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

button1.setLabel("Add");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
button1ActionPerformed(evt);
}
});

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

add(textField3);
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
try
{
n_First = Integer.parseInt(strField1);

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

//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
String strResult = "" + result;
System.out.println("Result = " + strResult);
textField3.setText(strResult);
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @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;
}

}

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

echarcha
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.

Charchila
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.