• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Prompt before Exit in Single Frame Application  RSS feed

 
Sean Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all,

Hoping someone knows the answer to this one. I have a Single Frame Application and I simply want to prompt the user with a dialog confirming they really want to close before they actually close the program, specifically when they click the X in the upper right. I THOUGHT it was like any other java application, with the slight added complexity of having to target the program-created-JFrame with: ".getApplication().getMainFrame()".



With that code, DO_NOTHING_ON_CLOSE is set on the JFrame, yet the program closes anyways. Which I don't understand.

So I did some reading and found this: http://bellquel.bo.cnr.it/appframework/org/jdesktop/application/SingleFrameApplication.html

That lead me to the configureWindow override, where I successfully tried this:



But as far as I can tell that will only let me react to the window closing, not to the command to close it, where I could then kill the command in the dialog if they so "no I'm not ready to quit yet", if that makes sense.

So, does anyone know how to give a prompt before closing?

Edit: I am using netbeans IDE to create this app - not sure if that is relevant, since maybe it is an oddity with the IDE and not Single Frame Applications.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Inside your window closing prompt the user for confirmation (check out the convenient static method(s) in the JOptionPane class)
If he does not confirm, do nothing.
If he confirms invoke System.exit(0);
 
Sean Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, I can do something like this:



But this doesn't intercept the shutdown if the person clicks "OK" instead of "Close Anyway." How do I actually stop the shutdown, which, as far as I've been able to tell, seems baked in to the SFA?
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You set the default operation to DO_NOTHING_ON_CLOSE, and just exit the application programmatically through the windowClosing event. Here are some ways you can close the frame:
 
Sean Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate the indepth example. The problem is that DO_NOTHING_ON_CLOSE doesn't seem to be working, and I suspect it has something to do with this being a java Single Frame Application. This line is already in my code:



By printing MyApp.getApplication().getMainFrame() to the log, I can SEE that DO_NOTHING_ON_CLOSE is successfully being set on that frame. And yet the X is still closing the frame. I seem to be targetting the wrong JFrame, but according to the SFA API,

"[Class SingleFrameApplication] takes care of component property injection, exit processing, and saving/restoring session state in a way that's appropriate for simple single-frame applications. The application's JFrame is created automatically, with a WindowListener that calls exit() when the window is closed."
"Note that the JFrame that's implicitly created by the show method is named "mainFrame"."

So I guess I should modify my question: Why is DO_NOTHING_ON_CLOSE failing for me?

 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm starting to realize that "Single Frame Application" isn't just an odd choice of capitalization, it actually refers to something specific. Originally I didn't pay any attention to it because I've never ever written an application with more than one frame, but apparently I should have. So, is that a product or a framework or something?
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that the frame already has a WindowListener on it that's shutting down the application. This completely bypasses DO_NOTHING_ON_CLOSE. What you can try is to remove all WindowListeners on your frame, before adding your own.
 
Sean Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, check the link in my first post for more information about Single Frame Applications.

The app extends SingleFrameApplication:



The problem is, it seems that the window close event is created by the SFA, and I can't for the life of me figure out where it is to remove it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:What you can try is to remove all WindowListeners on your frame, before adding your own.


The operative words here are "remove", "WindowListeners" and "frame".
 
Sean Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The operative words here are "remove", "WindowListeners" and "frame".


Yes, but how? I have been digging off and on for days trying to figure out how to remove the default SFA WindowListener, but to no avail. I know WHAT I must do, I just cannot figure out HOW to do it.
 
Sean Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
8 days later, I found this: http://www.oracle.com/technetwork/articles/javase/index-141957.html#lifecycle

Which more or less answers the question.

Relevant Bits, Code Example 5 in particular:

Application Exit and Shutdown
The Application class implements an exit method to gracefully shut down the application. According to the Application implementation, a graceful shutdown involves asking any ExitListener objects whether exiting is possible, then alerting those same listeners that the Application will actually shut down, calling the shutdown method, and finally calling the System.exit method.

But the Application class does not call the exit method directly. Your application should do this if it subclasses the Application class. However, the SingleFrameApplication class does this for you when you close the main window frame. It implements a WindowListener that calls the exit method when you close the application's main window frame. Regardless of how you finally call the exit method, you should override the shutdown method to perform application-specific cleanup before the application terminates completely. The shutdown method is your opportunity to close database connections, save files, or perform any other final tasks before your application finally quits.

The SingleFrameApplication superclass implements a simple shutdown method. It saves its window-frame session state and includes all secondary frame state as well. For this reason, you should remember to call super.shutdown() if you override this method. Code Example 4 shows you what to do.

Code Example 4

@Override
protected void shutdown() {
// The default shutdown saves session window state.
super.shutdown();
// Now perform any other shutdown tasks you need.
}

Implement the Application.ExitListener interface to allow your application the chance to veto or approve requests to exit the application. The default exit algorithm includes calls to all listeners before calling the shutdown method. By implementing the ExitListener interface, you can alert your customers or users of the impending shutdown operation and even allow them to stop the shutdown.

The ExitListener interface has two methods:

public boolean canExit(EventObject e)
public void willExit(EventObject e)
Use the canExit method to respond to the exit request. Return a true value to allow the exit, false otherwise. The willExit method is simply an alert notification, but you can also use it for any preparations you need for the ensuing shutdown.

Code Example 5 shows how you might implement an ExitListener object. Notice that the example calls the exit method, which is implemented by the Application superclass. The exit method notifies all ExitListener objects and calls the shutdown method only if all listeners approve the request to exit.

Code Example 5

public class ConfirmExit extends SingleFrameApplication {
private JButton exitButton;

@Override
protected void startup() {
getMainFrame().setTitle("ConfirmExit");
exitButton = new JButton("Exit Application");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exit(e);
}

});
addExitListener(new ExitListener() {
public boolean canExit(EventObject e) {
boolean bOkToExit = false;
Component source = (Component) e.getSource();
bOkToExit = JOptionPane.showConfirmDialog(source,
"Do you really want to exit?") ==
JOptionPane.YES_OPTION;
return bOkToExit;
}
public void willExit(EventObject event) {

}
});
show(exitButton);
}

@Override
protected void shutdown() {
// The default shutdown saves session window state.
super.shutdown();
// Now perform any other shutdown tasks you need.
// ...
}

/**
* @param args the command-line arguments
*/
public static void main(String[] args) {
Application.launch(ConfirmExit.class, args);
}

}

Code Example 5 defines a SingleFrameApplication that contains a single JButton in its main frame. When you click that button or attempt to close the main window, the application's ExitListener confirms the user request. Figure 2 shows the application responding through its listener interface.


Thanks to everyone for getting me looking in the right places.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through the documentation for JFrame, you should find methods which allow you to get all the Window Listeners, and to remove a Window Listener.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!