Article View: borland.public.jbuilder.java.swing
Article #8779How to cancel termination of application?
From: X_Aki Nieminen_X
Date: Thu, 20 Jul 2000 00:00
Date: Thu, 20 Jul 2000 00:00
85 lines
3597 bytes
3597 bytes
Thanks for your comments John, now I know a bit more about Java's internal
life.
Now I am having problems with the following feature. I want to cancel
termination of application in "windowclosing" event, but it does not work as
expected.
If I select "yes" from the confirmdialog, then application is terminated
(windowclosing and windowclosed handlers are called).
If I select "no" from the confirmdialog, then application keeps running BUT
still main window dissapear from the screen. Main window is a subclass of
JFrame.
How to cancel terminaton and still keep main window visible on screen?
====
[Snippet from code]
void mnuitmExit_actionPerformed(ActionEvent e) {
// this will invoke "windowclosing" handler
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
void this_windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(this,
"Do you want to close application?",
"Exit",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) ==
JOptionPane.YES_OPTION) {
System.out.println("Exit...");
this.dispose(); // release native window resources and invokes
"windowclosed" handler
} else {
// cancel exit and continue running application
this.setVisible(true);
System.out.println("Cancel...");
}
}
void this_windowClosed(WindowEvent e) {
System.out.println("Exit done.");
System.exit(0);
}
====
John P. McGrath kirjoitti viestiss๏ฟฝ ...
>On Wed, 19 Jul 2000 12:50:49 +0300, X_Aki Nieminen_X wrote:
>
>> If I call "this.dispose()" inside button handler, then
>> "windowClosing" event handler is never called, but only
>> "windowClosed" one.
>>
>> Finally, I created explicitly a new WindowEvent and
>> dispatch it to dialog instance. See my code below with
>> dispatching code.
>>
>> Is it legal to do it this way in Java to handle window
>> close operation, can I dispatch window message without
>> any side effects? Is there some better and efficient
>> way to this?
>
>I think you got it right.
>
>The windowClosing() event is poorly named, which is the source of much
>confusion. It is sent by the operating system to request that the window
>be closed. This occurs on Windows when you press the close [x] button,
>choose "Close" from the system menu, or press Alt+F4. Other operating
>systems have their own ways to send the event.
>
>The windowClosing() event does not actually close the window -- it is
>your dispose() call that closes it. If you want, you can ignore the
>event, and then the close button will do nothing.
>
>Since you call dispose(), the window is closed. The operating system
>then notifies your program that the window has been closed, and Java lets
>your code know by sending the windowClosed() event.
>
>If the AWT designers had thought about it a little more, they would have
>used a name like windowCloseRequest() rather than windowClosing(). This
>would have avoided a lot of confusion.
>
>--
>Regards,
>
>John McGrath
Technical Details
Message-ID:
<3976f4a9@dnews>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<397576c9@dnews> <MPG.13e0477786e74b8b989a4e@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,1dadf17715fc8d0f X-Google-Attributes: gid111d45,public From: "X_Aki Nieminen_X" <no@email.abc> Subject: How to cancel termination of application? Date: 2000/07/20 Message-ID: <3976f4a9@dnews> X-Deja-AN: 648592961 References: <397576c9@dnews> <MPG.13e0477786e74b8b989a4e@forums.borland.com> X-Trace: 20 Jul 2000 05:46:33 -0800, 193.209.12.92 Newsgroups: borland.public.jbuilder.java.swing X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3