Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ivan Jouikov
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic