views:

33

answers:

1

I am trying to write a GUI application using NetBeans, and I am getting increasingly tired of it..

I constructed a "Java Desktop Application" using NetBeans. It creates the main form as a org.jdesktop.application.FrameView descendent. I would like to know what is the proper way to add some code that is executed when the form is closed.

My current attempt so far is to add a WindowAdapter object using getFrame().addWindowListener in the constructor, which doesn't work. Because you can't call getFrame while the frame is not constructed yet. And I can't see it as an event somewhere i the GUI builder.

+1  A: 

Java Desktop Application which is available in NetBeans IDE 6.9.1 is only for historical purposes and is not recommended for use in projects. The NetBeans IDE 6.9.1 also shows this warning when we try to create a new project using Java Desktop Application option.

alt text

Given that let me answer your question assuming you are still using the Swing Application Framework and you want to add a windowClosing listener to the Main Window.

When you create a Java Desktop Application you get three classes (assuming you typed DesktopApplication1 as the name of your application):

  1. DesktopApplication1.java
  2. DesktopApplication1AboutBox.java
  3. DesktopApplication1View.java

To add the window closing listener, write the code in configureWindow method of the class DesktopApplication1 as follows:

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

    root.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            // write your code here
            System.out.println("Window Closing"); 
        }

    });
}

with regards
Tushar Joshi, Nagpur

Tushar Joshi
Thanks a lot. I never noticed the deprecation of the swing application framework.
Muhammad Alkarouri