• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple example of passing a variable between 2 forms

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks is advance

John
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What have you got so far?
 
A John Peters
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


and
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Enter Data
    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

    Thanks
    John
     
    Campbell Ritchie
    Marshal
    Posts: 79178
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Start application
    Start processing class
    Processing class shows form
    etc

    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic