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.
解决方案
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.
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):
DesktopApplication1.java
DesktopApplication1AboutBox.java
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