aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes SWT: How to prevent  window from closing when clicking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "SWT: How to prevent  window from closing when clicking "X"?" Watch "SWT: How to prevent  window from closing when clicking "X"?" New topic
Author

SWT: How to prevent window from closing when clicking "X"?

Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Hi!

Basically, I want to implement the following functionality:

When the user wants to close the window by clicking X button, I wish to popup a message saying "do you wish to save your crap, YES | NO | CANCEL". I did this by adding a disposeListener on the shell...

Now I am trying to figure out how to prevent disposal of the shell in case the user clicks "CANCEL"... Any ideas?
Nick Kocher
Greenhorn

Joined: Nov 24, 2004
Posts: 14
hi Ivan,

What is a disposeListener?

alternatively you could add a windowListener and then in windoClosing() show the popup dialog and check what the user clicks
if YES
//do something and then
dispose() your program or System.exit(0);

if NO
call dispose() on your program;

if CANCEL
dispose() your dialog
//and do nothing

I don't know may be there is a better way to solve your problem
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
Thanks for the clue, but I need more information...

Do I add the windowListener to my main shell?

And what's the windowClosing() method? Is it one of Shell's methods? As far as i know Shell can't be overriden, so where is it then?

I'll play around with it later today, just thought I'd ask you up front
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nick seems to have missed the fact that this is an SWT question, not a Swing question. I think Ivan, you can safely ignore his post.

Anyway, this is a good question, Ivan, and I wish I knew the answer. I think I'd try to figure it out by looking at the JFace "ApplicationWindow" class.


[Jess in Action][AskingGoodQuestions]
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
What if I'm not using JFace? My App was built on pure SWT, so if is there a simple way to wrap main components (such as the Shell) into JFace?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I was suggesting that you browse the source starting with the ApplicationWindow class.

Anyway, I just has a look around the source for Shell, and it looks as though you can do at least one of two things:

1) Subclass shell and override Shell.close(). In your override, you could post the dialog box, and if you want to really exit, call super.close(); otherwise, don't.

2) Don't subclass Shell, but instead add a ShellListener to the Shell which handles SWT.Close events by setting the Event.doIt field to "false".

I'd try the second one, and if it doesn't work, try the first one.
Ivan Jouikov
Ranch Hand

Joined: Jul 22, 2003
Posts: 269
The second option worked, thanks for your help.

The biggest problem that I have is that I don't know where the hell SWT javadoc is... There is

http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.doc.isv/doc/reference/api/

But it's old, VERY old.

Do you know where i can find a current one?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, inside of Eclipse, you can pick Help | Help Contents, and then in the help viewer open "Platform Plug-in Developer Guide", then Reference, then API Reference, and you've got the whole org.eclipse Javadoc tree. I'm sure there's a separate download somewhere, but I can't tell you where off the top of my head.

But I actually rarely look at it -- I tend to just look at the platform source.
 
 
subject: SWT: How to prevent window from closing when clicking "X"?