0

I am writing an application (an implementation of the Game of Life) and want it to work as follows:

  1. A JFrame (not a JDialog!) pops up where the user can choose some settings.
  2. 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).

Community
  • 1
  • 1
andreasdr
  • 3,804
  • 4
  • 28
  • 32
  • 1
    I would recommend you to read about MVC model. You would be able to do all this by your controller, which will solve all your problems. – Blood Nov 04 '12 at 12:11
  • *"A JFrame (not a JDialog!)"* Why? – Andrew Thompson Nov 04 '12 at 13:07
  • @AndrewThompson: I want to learn what to do in a case where a JDialog is not a viable alternative, even if it might be the most convenient choice in this particular case. – andreasdr Nov 04 '12 at 13:16
  • *"where a JDialog is not a viable alternative"* Name one instance (that does not also rule out a `JFrame`). – Andrew Thompson Nov 04 '12 at 13:18
  • @AndrewThompson: Suppose I want to start a separate JFrame from my main-method that represents a more or less advanced application, which contains a "Save data"-button. When this button is clicked, I want the application to transfer some data to be stored in the class containing the main method (and keep running). – andreasdr Nov 04 '12 at 13:28
  • 2
    Sounds like bloody GIMP. A great idea in the mind of the programmer, a nightmare of usability. I take it you have seen my official answer on such atrocities, explored in.. [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Nov 04 '12 at 13:32

1 Answers1

2

You need to register a callback from the main method to the settings frame. When the Start button is pressed, the action listener should close the window, and invoke the callback, passing the settings entered in the settings frame. The callback method will then open the game of life frame.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    @andreasdr there I can't see any reasons for another JFrame or JDialog, use CardLayout, why to reinvent the wheel – mKorbel Nov 04 '12 at 14:43