I am looking for a simple example of passing information from 1 form to another form.
Assume I have a form that has 2 text fields and a go button. I enter data into the each of the text fields. When I hit go, the action listener converts the 2 text fields to decimals, add them together and opens a new form. The new form displays the 3 variables. I would like to use this sample for more complex problems etc.
Can use swing or awt. Using Eclipse as my IDE. I do not want to use a dialog box for this example.
Kind of convoluted at this time but here it is. Be gentle, I am learning, that is why I want a simple example to work with.
Joined: Oct 13, 2005
I think you are going the wrong way about it. You said you want a form to send information to something. But I think you need a class which takes information and displays it.
1. Set up a class with a main() method.
2. Get that to call a second class to do the arithmetic, creating an instance of it.
3. Supply that instance with values, input from the keyboard, to add.
4. Add your GUI. You can have it referenced from the class with the main method in (or other locations).
5. Now you can create an ActionListener which passes the information in the text boxes to that same method.
7. You can use the same methods you passed the information from the keyboard to receive the information from the GUI.
8. Now show your output GUI.
Give your GUI methods like getValueFromNumberBox1() and setValueInNumberBox(). Keep your GUI for the display. Get the controlling and the model (the adding of the values) out of the display.
Anybody else? I don’t think I have explained this at all well, and somebody else would probably do a lot better.
A John Peters
Joined: Oct 17, 2011
This looks like a 1 form solution. I really want to learn how to communicate between 2 forms in one application. It is more of a learning exercise than a real app.
The steps I would like to learn
Show a form
Do Something with the data
Show the data and results on a new form
Based on what shows up, let the user do something else
Joined: Oct 13, 2005
Start processing class
Processing class shows form
Now, you have another class which instantiates both the input frame and the output frame, but the output frame is setVisible(false) so you can’t see it. Pass an instance of the Arithmetic class to the input frame somewhere, so its button’s ActionListener calls the takeInput method, then the displayOutput method. Pass an instance of the output display to the Arithmetic class (the way I have written it, it can’t be null ever), then the displayOutput makes the output display appear.
Obviously my Arithmetic class is simplified and almost certainly not compatible with your display classes, but I am just trying to give you the general idea.
You must make sure all these classes are called only in the Event Dispatch Thread, because they are not thread-safe.