• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to return values from a JForm to the main window on close?

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am making a java swing program. I have a main window that opens a JForm window when the user clicks a menu option. Then I have a button on the JForm that closes the window on click. However, I would also somehow like to make the JForm return values to the main window (according to the states of the selector components) when it is closed. How would I go about this? Currently my jForm is a seperate class called "NewGame", and inside the menu item mouse clicked method, I have the following code:



Is there something I can add here to get the object's values upon the window's close? Or is there a way I can add that into the button clicked method on the actual jForm? Help would be much appreciated.
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call the JForm, you could pass a reference for the main window (or a reference for an interface implemented by the main window with methods that the JForm is allowed to call). When the button is clicked to close the JForm, it could call back to the main window with the data collected in the form, then close the window.

For example:





Runtime output:
Updatable value changed to: new value
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, unfortunately I don't really understand your examples (I'm a beginner, so it's probably my fault, but specifically I don't see how your example relates to doing something upon the close of the jForm. I know I could add a window closing event to the JForm itself, but then I don't know what I would put inside that event to return values to the main class, and I know how to access the jForm values from the main class, but I don't know how to add an event to the main class that is called when the jForm closes.

"When the button is clicked to close the JForm, it could call back to the main window with the data collected in the form, then close the window"

This sounds like you know how to do what I'm trying to do, but I don't understand how to "call back to the main window with the data collected in the form."

Thanks
 
Ron McLeod
Saloon Keeper
Posts: 1263
131
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is that the MainFrame implements the MessageListener interface. MainFrame creates an instance of OtherFrame and passes-along a reference to itself. When OtherFrame has a message to send to MainFrame, it calls-back to MainFrame's onMessage method via the interface using the reference provided.

This code is not complete and will not run as-is, but it should give you some ideas to try.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:The idea is that the MainFrame implements the MessageListener interface. . .
Not convinced.

The intention of a Frame is to be a display. Don't make it do multiple tasks. You should have another class to send the messages to. In fact you should be able to use that other class to send messages to and from the command line, without putting a GUI on top of it.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I still don't understand is how to get the main class to look at the values of the data class as soon as the JForm is closed. I could add a window closing event to the actual JForm itself, but what I really want is for the main class to know when the JForm is closing, and to get all of its values right before it closes.

Your answers seem to be about where the correct place to send the data is, but my problem is how to get the program to send the data at the correct time.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are bound to be lots of possible solutions. Two spring to mind; they might not both be suitable for you.

You can add a Listener to the frame (I think WindowListener‍←link) and invoke its methods when the frame closes.
You can be adventurous and learn about shutdown hooks. They look really complicated, but they are quite simple to use once you understand them. When the runtime closes, you can start a new thread which writes all the data to a file.
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the problem with a shutdown hook is that I don't want to get the values from the JForm when the entire program is shutdown, just when that window is closed. Maybe I'm misunderstanding shutdown hooks, but it sounds like they are a way to run code when the JVM is shut down (like when you call System.exit(0)). Thus, the problem is that I want the code to run when I call dispose() on the window, but not System.exit(0).

And the problem with a window listener (using the window closing event) is that I don't know how to add one to the main class that listens to the window in the other class. I know how to add a window closing event to the JForm itself, but then don't know what commands to write to return the values to the main class. I know what commands to write in the main class to access the values of the JForm, but I don't know how to create a windows listener to the main class that listens to the JForm window, only one that listens to the main window.
 
Campbell Ritchie
Marshal
Posts: 52516
118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorials
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that makes sense, except for the actual writing to a file part. I think I could figure out how to write the data to a file, but in your example only the JForm knows when its been submitted, not the main window. How do I tell the main window to access the data from the file as soon as the JForm is closed?
 
Paul Clapham
Sheriff
Posts: 21862
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a lot of people are waffling here because they have no idea what a JForm is. I'm one of them -- JForm isn't part of the standard Java API. So where did you get it? Can you post a link to its API documentation?
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To tell the truth, I'm not exactly sure. Just something I found in the netbeans GUI creator. However, assuming that the main window opens another window that's supposed to function as a form, my question still stands. I know how to run code in the auxiliary window when it is about to close, but I don't know how to send the values from the auxiliary to the main window from inside this window closing event. And conversely, I know how to access the values in the auxiliary window from the main window, but I don't know how to make the main window to wait to access the values until the auxiliary window has been closed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!