2

My application shall switch programmatically from one to another main Window (that means the first window should get closed, then the second window should get opened).

The switch shall be executed within an ActionListener object which is added to window1. The actionPerformed(ActionEvent e) method here looks like this:

@Override
public void actionPerformed(ActionEvent e) {
    WindowStarter.closeConfigWindow();
    WindowStarter.openMainWindow();
}

where closeConfigWindow() is:

public static void closeConfigWindow() {
    if (window1 != null)
        window1.dispose();  
}

In the java-doc you can find the following information related to the dispose() method of the Window class:

Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information

So my question is:
Do I risk that the application terminates when the method gets called?
(because: the Listener Object is only related to the instance of window1, after disposing that window, all objects related to that instance are "off the JVM hook". So the application should terminate theoretically?)
Or asked otherway round:
Why doesn't the application terminate despite that?

thanks in advance

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) Either the 2nd frame should be a dialog, or there should be a single frame using a `CardLayout`. – Andrew Thompson Nov 11 '14 at 22:30

3 Answers3

1

You can set JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); and implement your own WindowListener, to handle the closing operation.

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • remember that I want to do the switch from one to another window programmatically. (I set defaultCloseOperation on DO_NOTHING_ON_CLOSE and I implemented my own WindowListener.) What I am interested in is: after calling dispose() on the window, without opening a new window the application terminates. when opening window2 after closing window1 like described in the code, the application doesn't termiante. I want to know why it doesn't terminate despite the information of the java-doc that the program terminates after the last window has disposed ... – matthiasboesinger Nov 11 '14 at 14:29
1

If you have non-daemon threads running (main or any other thread you created and didn't set it to be a daemon thread), the application will not terminate when you close a frame with defaultCloseOperation set to DO_NOTHING_ON_CLOSE.

Taking a look at the documentation referred on Window API, it says:

if you require the JVM to continue running even after the application has made all components undisplayable you should start a non-daemon thread that blocks forever.

So, exiting the application when all components are disposed is implementation dependant, libraries can cause trouble and you need to correctly dispose everything and it can have pitfalls like this, where the guy was calling SwingUtilities.invokeLater from a daemon thread and it was preventing the application to exit cleanly.

But, if you have non-daemon threads running, the application will not exit even if all GUI is disposed.

Jean Waghetti
  • 4,711
  • 1
  • 18
  • 28
0

@ Jean Waghetti: Thanks for your hint and the informations!

The application will not terminate, when using the posted code, because one necessary condition for AWT/Swing based applications to terminate is, that the AWT-EventQueue is empty.

Here is the crucial passage:

Starting with 1.4, the behavior has changed as a result of the fix for 4030718. With the current implementation, AWT terminates all its helper threads allowing the application to exit cleanly when the following three conditions are true:

There are no displayable AWT or Swing components.
There are no native events in the native event queue.
There are no AWT events in java EventQueues.

In my case, calling window1.dispose(); will not terminate immediatly, because the call is made out of a actionPerformed method. By doing so, there will be at least one Task in the EventQueue, and the application will finish this task, even if the related window is disposed during this task (it is also possible to invoke further Actions of this window, even if it has been disposed already).

Furthermore: When calling dispose() in a AWT-Event (and the disposed window was the last window), there will be a 1000 ms timeout after the last task has been finished, before the program will shutdown. (In contrast: When closing a window via the 'X' button, the program terminates immediatly).

so far ...