Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2004

Tip: Looking for answers? Try searching our database.

Stopping close in windowClosing

Thread view: 
Soeren - 30 Sep 2004 12:24 GMT
Hi

I have an application where I would like to ask the user if (s)he wants to
cancel current action. The close is issued by clicking the [X] mark in the
(XP) Window. I use WindowAdapter.windowClosing in order to respond to this
action.

My problem is that I dont know how to stop the window close if the user
responds that (s)he does not want to cancel.

Can you assist? Thanks in advance.

Best regards

Soeren
Dirk Gerrit Oort - 30 Sep 2004 13:15 GMT
try something like this:

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Window extends JFrame
{
 public Window()
 {
   this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
   this.addWindowListener(new WindowAdapter()
       {
         public void windowClosing(WindowEvent e)
         {
           WinClose(e);
         }
       });
 }

 private void WinClose(WindowEvent e)
 {
   int ok = 0;
   int cancel = 2;
   int anwser = JOptionPane.showConfirmDialog(this,
       "Do you want to close", "information",
       JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

   if (anwser == ok)
     System.exit(0);
 }
 public static void main(String[] args)
 {
   Window w = new Window();
   w.setSize(200,200);
   w.setVisible(true);
 }
}

Signature

D.G. Oort

> Hi
>
[quoted text clipped - 11 lines]
>
> Soeren
Soeren - 30 Sep 2004 13:36 GMT
Hi Dirk,

Thanks for your answer. Maybe I havent explained myself properly. What I
really want is to be able to cancel the close. Assume that the user presses
the [X] and gets the question "Do you really wish to cancel". If (s)he
responds no, the window should not be closed.

> try something like this:
>
[quoted text clipped - 52 lines]
> >
> > Soeren
Tor Iver Wilhelmsen - 30 Sep 2004 14:14 GMT
> Thanks for your answer. Maybe I havent explained myself properly. What I
> really want is to be able to cancel the close. Assume that the user presses
> the [X] and gets the question "Do you really wish to cancel". If (s)he
> responds no, the window should not be closed.

Well, if you don't close it yourself, you have forgotten to change the
default close operation, which is to close it. Set the default to
do nothing.
Soeren - 30 Sep 2004 16:55 GMT
Thanks for your answer,

But how can I react on that the [X] is pressed, if it as default is set to
do nothing?

> > Thanks for your answer. Maybe I havent explained myself properly. What I
> > really want is to be able to cancel the close. Assume that the user presses
[quoted text clipped - 4 lines]
> default close operation, which is to close it. Set the default to
> do nothing.
Paul Lutus - 30 Sep 2004 17:04 GMT
> Thanks for your answer,

Do not top-post.

> But how can I react on that the [X] is pressed, if it as default is set to
> do nothing?

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
   public void windowClosing(java.awt.event.WindowEvent evt) {
       yourCodeHere(evt);
   }
});

Signature

Paul Lutus
http://www.arachnoid.com

Andrew Thompson - 30 Sep 2004 17:06 GMT
> Thanks for your answer, ..

Who?  Please refrain from top-posting Soeren.
<http://www.physci.org/codes/javafaq.jsp#netiquette>

> But how can I react on that the [X] is pressed, if it as default is set to
> do nothing?

Consulting the Javadocs might be a good place to start,
but you are possibly knobbling yourself by using a WindowAdapter
in the first place.  implement WindowListener on the class
and you will gain full control.

A couple of clarifications though.  
Are you dealing with Swing or the AWT?  
Is it a frame or window that you are dealing with?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Andrew Thompson - 30 Sep 2004 17:13 GMT
> But how can I ...

As an aside.   The example provided by Dirk displays
*exactly* the behaviour you claim to want.  Did you
actually try it?

If that does not work for your problem, you need to
explain your problem again.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Paul Lutus - 30 Sep 2004 17:16 GMT
>> But how can I ...
>
> As an aside.   The example provided by Dirk displays
> *exactly* the behaviour you claim to want.

So it does. The OP now has two examples of the same code. :)

Signature

Paul Lutus
http://www.arachnoid.com

Andrew Thompson - 30 Sep 2004 17:37 GMT
>> As an aside.   The example provided by Dirk displays
>> *exactly* the behaviour you claim to want.
>
> So it does. The OP now has two examples of the same code. :)

Maybe he's going for a 'hat-trick'..   ;-)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.