Click to See Complete Forum and Search --> : About windowClosing event...


DodgerLD
April 6th, 2004, 04:36 PM
Hi,

I have created a class that extends WindowAdapter which allows me to prompt the user when he/she leaves the program.

I override the windowClosing event and ask the user whether or not they really wish to exit the application.

2 Questions:

1. If they choose to exit, I simply System.exit(0), but what do I do if they choose not to? How do I cancel the execution of the windowClosing method?

2. This works if the user closes the application using the titlebar, but I also have an exit option in the menu, which calls System.exit(0). But this doesn't trigger the windowClosing event, how do I go about doing this, so anytime the program is closed, the user is prompted for confirmation?

TIA.

D.

mikeBarr81
April 6th, 2004, 06:02 PM
For the first question, i don't think you can cancel the window closing operation (i may be wrong). To get around this you should change the default window closing operation from EXIT_ON_CLOSE to DO_NOTHING_ON_CLOSE or HIDE_ON_CLOSE. You can then catch the events and ask the user if they really want to exit, and if not the window won't actually close.
For the second question you need to put the code that pops up the question box in a seperate method (listener method that catches the event), then you can call it from the listener method that catches the even for pressing the x AND from whatever handles choosing the exit option from menu.

ats007spdou
April 7th, 2004, 02:59 PM
Originally posted by DodgerLD
1. If they choose to exit, I simply System.exit(0), but what do I do if they choose not to? How do I cancel the execution of the windowClosing method?


You can display a JOptionPane, which will force the user to decide if he wants to exit the program or not. I made it in the below example by having the option pane attached to the JFrame that displays the main page and then adding some features to it(check out the JavaAPI for the full list of features and capabilites of the JOptionPane).


// this is a function that can be called by a menu or a button(even from the system title bar)
private void exitMethod()
{
// gets the text from the screen and so that it could be compared to
// earlier text in an attempt to determine if the warningToSave window
// should be shown
// opens up a window which prompts a user to save
if(mainPane.getText().compareTo(aB.receiveData()) != 0)
{
b = JOptionPane.showOptionDialog(CentralClassFrame.this,
"Would you like to save your previous work?", "Warning",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE, null, a, a[0]);

if (b == JOptionPane.YES_OPTION)
{
save();
System.exit(0);
}
else if (b == JOptionPane.NO_OPTION)
{
System.exit(0);
}
else
{
return;
}
}
else
{
System.exit(0);
}
}



DodgerLD
2. This works if the user closes the application using the titlebar, but I also have an exit option in the menu, which calls System.exit(0). But this doesn't trigger the windowClosing event, how do I go about doing this, so anytime the program is closed, the user is prompted for confirmation?


In your main method...

public class CentralClass
{
public static void main(String[] args)
{
CentralClassFrame f = new CentralClassFrame();
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.show();
}
}

class CentralClassFrame extends JFrame
{
..................................
}


...you should set the JFrame default close operation as DO_NOTHING_ON_CLOSE, so that nothing happens when the user wants to close the main JFrame by clicking on the little 'X' on the top-right hand corner. When you select the option from you menu(or click on a button), you can have it pop-up a dialog pane as an action of that particular menu item. And as for the system close option, you'll have to add a window listener, which will pick up on anything that happens in the system bar, as shown below...

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
aC.play();
exitMethod();
}
});


Hope this helps :wave:

DodgerLD
April 15th, 2004, 02:59 PM
Thanks guys! :)

I see you can call an event handling method such as windowClosing() directly, I never realised that.

ats007spdou,

In your last code snippet you declare a method at the same time that you are assigning an object value. What exactly is this called? I would like to read up about this technique.

Thanks again,

D.

mikeBarr81
April 15th, 2004, 04:02 PM
In your last code snippet you declare a method at the same time that you are assigning an object value. What exactly is this called? I would like to read up about this technique. Do you mean this code snippet?
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
aC.play();
exitMethod();
}
}); If so, then ats007spdou isn't just declaring a method but actually a full class! It's known as making an anonymous class and is often used with listeners where the listener is unique to a particular button and is never used anywhere else. It saves making an extra .java file. The class is anonymous because you don't give it a name, but it will produce a .class file like any other. The name of the .class file will be related to the class that it was made in. Obviously it's only really useful if the listener is short. That is why ats007spdou used a window adapter, so all of the other methods didn't have to be defined.

DodgerLD
April 16th, 2004, 03:46 PM
Sorry, I meant declare a class :rolleyes:

I see they are a form of inner class.

Thanks so much for the help ;)

D.