1

So I'm fairly new to Java Swing, and I stumbled upon a certain difficulty. I have a main Frame running (main part of the application that is visible throughout the app's execution) which has a button that once clicked, invokes a popup Frame (window) to collect user's information, and that frame has some Components. The problem being is that I don't really know the right approach to invoking the popup window and the main window freezing the execution and waiting until OK, or cancel button is clicked on the popup. Once this happens the main window code collects the returned values from the popup and resumes. I tried using synchronization to accomplish this, however the popup components don't even load, just the JFrame and JPanel (white background) and the popup freezes up on the wait() condition. I know that there is a way of doing it with JDialog and others, my main concern however, is to discover why the popup frame doesn't load the components and freezes up before the wait() condition. (when I get rid of wait() everything loads properly).

    //in Main window Class:
    frame.setEnabled(false);
    Test test = getNewTest(); //should freeze on wait() in popup window
    frame.setEnabled(true);

    //in Popup Window Class
    public Test getNewTest() {

    addPanel.setVisible(true);
    addFrame.setVisible(true);

    synchronized(flag) {
        try {
            flag.wait();
        } catch (InterruptedException e) {}
    }

    addPanel.setVisible(false);
    addFrame.setVisible(false);

    if(success)
    return new Test(testName, date);
    else return null;
}


    //When OK or Cancel button clicked appropriate handler sets
    //success value and invokes flag.notify();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Gruxer
  • 13
  • 2
  • 3

1 Answers1

2
  • Get all that synchronized and wait stuff out of your code. All that will do is freeze the Swing event thread, rendering your application useless.
  • You don't want to use a second JFrame, since an application typically has only one main window or JFrame, not multiple.
  • You want instead to use a modal dialog such as a JOptionPane (which can hold complex GUI's), or a modal JDialog (which also can hold a complex GUI). Be sure to associate the modal dialog with the parent JFrame. The Swing GUI library will then freeze the main window until the dialog has been dealt with, and the code from the main GUI will resume from the place the dialog was made visible after the dialog is no longer visible.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    Minor clarification: a modal dialog doesn't _freeze_ the main window, but it does _block_ user interaction, as suggested [here](http://stackoverflow.com/a/18728637/230513). – trashgod Nov 19 '13 at 23:28
  • 1
    @trashgod: thanks for the clarification. But it does more than just block user interaction as it in fact stops program flow in the calling code right at the point where the dialog is set to visible. Program flow resumes from that very same point once the dialog is dealt with. – Hovercraft Full Of Eels Nov 19 '13 at 23:32
  • 1
    Good point. I see that the tutorial uses two phrases to describe this: _blocks input_ and _captures the window focus_. I meant to underscore how the main window can still update its appearance in response to events. – trashgod Nov 19 '13 at 23:42
  • 1
    @trashgod: indeed it can. But for my own projects, I note that it's crucial to add all listeners, such as SwingWorker PropertyChangeListeners, to the code before the dialog is set visible – Hovercraft Full Of Eels Nov 19 '13 at 23:43