I am writing an application (an implementation of the Game of Life) and want it to work as follows:
- A JFrame (not a JDialog!) pops up where the user can choose some settings.
- Pressing the "Start"-button on this frame stores the settings somewhere, closes it, and opens the Game of Life frame.
What is the best way to achieve this?
My current approach is to control the application from a separate class containing the main method, which is supposed to store the settings and handle the showing/hiding of the two JFrames, but I run into some problems: I want the listener for the "Start"-button in the settings frame to signal to the main method that it is time to store the chosen settings, hide this frame and show the GoL-frame. However, I don't know how to communicate this information to the main-method, since I don't have access to the scope of its class from within the "settings" frame. I do have access to the "settings" frame from the main method, so getting the settings and closing the frame is not a problem, the problem is to know when to do it.
//In the class "SettingsFrame"
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//WHAT TO DO HERE?
}
});
I'm somewhat of a beginner, so maybe there is something fundamentally wrong with my attempted approach. What is the most elegant way to achieve the functionality mentioned above without the use of a JDialog?
(I've looked around on SO and found this answer Passing values between JFrames . However, the attached example uses a JDialog, which I guess automatically signals to the calling method that its "OK" button has been pressed. I am interested to know how to achieve this myself).