R
Rexx Magnus
I've got a bit of a problem with an application that I'm writing - I'm
using Netbeans 6.1 (JDK5). The app needs to be totally cross-platform, and
as such because I work on it under Mac, Windows and Linux, I don't want to
be bound to one particular platform to compile it.
The reason that this is an issue is because when I quit the application,
I'm catching the window close event and saving various settings to a file
before it exits. On Windows and Linux this should be fine, however on the
Mac, if you exit it using the menu or command-q, it bypasses the
windowclose event, therefore not saving the data.
I'm assuming that the generic catch-all is to add an ExitListener to the
application, but because of the way that the Netbeans project is written
and generated, I'm not sure where to put the relevant code. I've tried
overriding the shutdown() method in the (App) and putting in a simple
system.out.println statement to see if it catches it. That doesn't appear
to work.
I've also tried putting code in the (View) in order to attach the
ExitListener, much in the same way as the WindowListener, but that doesn't
seem to work either.
The main program code is stored in the (View), as are the variables and
methods I need to access on shutdown. When I've tried using methods in the
(App) class, I can't call them due to not actually having a handle on the
instance - I'm guessing it's because of the way that Netbeans sets up the
GUI with show(new DesktopApplication1View(this));
Any help would be appreciated!
I want to avoid using the menu hooks and OS detection to catch the use of
the Apple menus, because this requires importing of apple packages,
meaning I'll have to alter it if I compile on other platforms (I'm
desperately trying to keep this platform agnostic).
The code that usually comes up for a GUI is something like this (with my
edits in):
(View)
-------------------
public class DesktopApplication1View extends FrameView {
public DesktopApplication1View(SingleFrameApplication app) {
super(app);
initComponents();
//I've added this for the window listener and also to make it
non-resizable
this.getFrame().setResizable(false);
this.getFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.getFrame().addWindowListener(new Closer());
//status bar code etc. that I didn't need taken out.
}
//about box code here - something else I don't use
//along with all the other gui generated code.
//my window closer
public class Closer extends WindowAdapter{
@Override public void windowClosing(WindowEvent e){
System.out.println("saving");
saveFile();
System.exit(0);
}
}
}
-------------------
Then you've got the code that actually calls the gui code:
(App)
-------------------
package desktopapplication1;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
/**
* The main class of the application.
*/
public class DesktopApplication1 extends SingleFrameApplication {
/**
* At startup create and show the main frame of the application.
*/
@Override protected void startup() {
show(new DesktopApplication1View(this));
}
/**
* This method is to initialize the specified window by injecting
resources.
* Windows shown in our application come fully initialized from the GUI
* builder, so this additional configuration is not needed.
*/
@Override protected void configureWindow(java.awt.Window root) {
}
/**
* A convenient static getter for the application instance.
* @return the instance of DesktopApplication1
*/
public static DesktopApplication1 getApplication() {
return Application.getInstance(DesktopApplication1.class);
}
/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(DesktopApplication1.class, args);
}
}
-----------------
using Netbeans 6.1 (JDK5). The app needs to be totally cross-platform, and
as such because I work on it under Mac, Windows and Linux, I don't want to
be bound to one particular platform to compile it.
The reason that this is an issue is because when I quit the application,
I'm catching the window close event and saving various settings to a file
before it exits. On Windows and Linux this should be fine, however on the
Mac, if you exit it using the menu or command-q, it bypasses the
windowclose event, therefore not saving the data.
I'm assuming that the generic catch-all is to add an ExitListener to the
application, but because of the way that the Netbeans project is written
and generated, I'm not sure where to put the relevant code. I've tried
overriding the shutdown() method in the (App) and putting in a simple
system.out.println statement to see if it catches it. That doesn't appear
to work.
I've also tried putting code in the (View) in order to attach the
ExitListener, much in the same way as the WindowListener, but that doesn't
seem to work either.
The main program code is stored in the (View), as are the variables and
methods I need to access on shutdown. When I've tried using methods in the
(App) class, I can't call them due to not actually having a handle on the
instance - I'm guessing it's because of the way that Netbeans sets up the
GUI with show(new DesktopApplication1View(this));
Any help would be appreciated!
I want to avoid using the menu hooks and OS detection to catch the use of
the Apple menus, because this requires importing of apple packages,
meaning I'll have to alter it if I compile on other platforms (I'm
desperately trying to keep this platform agnostic).
The code that usually comes up for a GUI is something like this (with my
edits in):
(View)
-------------------
public class DesktopApplication1View extends FrameView {
public DesktopApplication1View(SingleFrameApplication app) {
super(app);
initComponents();
//I've added this for the window listener and also to make it
non-resizable
this.getFrame().setResizable(false);
this.getFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.getFrame().addWindowListener(new Closer());
//status bar code etc. that I didn't need taken out.
}
//about box code here - something else I don't use
//along with all the other gui generated code.
//my window closer
public class Closer extends WindowAdapter{
@Override public void windowClosing(WindowEvent e){
System.out.println("saving");
saveFile();
System.exit(0);
}
}
}
-------------------
Then you've got the code that actually calls the gui code:
(App)
-------------------
package desktopapplication1;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
/**
* The main class of the application.
*/
public class DesktopApplication1 extends SingleFrameApplication {
/**
* At startup create and show the main frame of the application.
*/
@Override protected void startup() {
show(new DesktopApplication1View(this));
}
/**
* This method is to initialize the specified window by injecting
resources.
* Windows shown in our application come fully initialized from the GUI
* builder, so this additional configuration is not needed.
*/
@Override protected void configureWindow(java.awt.Window root) {
}
/**
* A convenient static getter for the application instance.
* @return the instance of DesktopApplication1
*/
public static DesktopApplication1 getApplication() {
return Application.getInstance(DesktopApplication1.class);
}
/**
* Main method launching the application.
*/
public static void main(String[] args) {
launch(DesktopApplication1.class, args);
}
}
-----------------