javaswingswingxswing-app-framework

Window closing in Swing Application Framework


I am using Swing Application Framework JSR(296) for my Swing based Java application.

Similar to AboutBox, I have followed the usage of @Action and added some JDialog classes to my project.

The problem is, when I close the main frame, my application still runs in background. To overcome this I added following code to the configureWindow() of my main application class:

protected void configureWindow(java.awt.Window root) {

    root.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
        // write your code here
            Application.getInstance(MyApp.class).exit();
        }
    });
}

But with this modification whenever I close the the dialog (including AboutBox), it also closes the main frame.

What should I do to prevent the entire application from exiting and just close the dialog box?

Update: I am using NetBeans IDE 7.01 which allows to create Swing Application Framework project. It generates a project skeleton as shown below:

MyApp 
|--myapp 
|   |--MyApp.java
|   |--MyAppAboutBox.java
|   |--MyAppView.java
|
|--myapp.resources
    |--MyApp.properties
    |--MyAppAboutBox.properties
    |--MyAppView.properties

NetBeans IDE allows to add actions from Window->Properties menu.

MyApp class extends org.jdesktop.application.SingleFrameApplication which is my main class.

MyAppView extends FrameView which is my main view.

Implementation classes of javax.swing.JDialog are in the myapp.view package.


Solution

  • The WindowEvent class has a method call getWindow(), which returns the window that is closing.

    Inside your windowClosing method you can check: if the window is the main application window, use the code that you currently have. If it is not, just call Window.dispose()

    Edit: I didn't notice that you were creating custom dialogs in your application. Maybe you forget to dispose them? You should add code like the one in the auto generated about box:

    @Action public void closeAboutBox() {
        dispose();
    }
    

    and call this action whenever the dialog closes. If this is not the problem, a thread dump will probably help you in order to find out which thread is running when you close the main window.