Solved

How do I override alt+f4 if "cancel" is chosen in my Java App?

Posted on 2008-06-20
6
529 Views
Last Modified: 2013-12-17
I am writing a text editor in Java using SWT and I am having a little trouble controlling the closing behavior. When exit is selected from the file menu of my app, the program follows this behavior

if there are unedited changes
ask: "save before close?"
if yes -> save, close
if no -> close
if cancel -> do nothing (program remains open)

The problem is that when the program is closed by the X (window close button) or Alt+F4 if the user chooses cancel the program still closes. I would like the program to do nothing if cancel is chosen, the same way it would if the user had selected exit from the file menu.

Thank you for the help!
- Nick

//My shell close listener from the app
 
		shell.addListener (SWT.Close, new Listener () {
			public void handleEvent (Event event) {
				kill();
			}
		});
 
 
//The behavior I want the program to follow when instructed to close by the user
 
	public void kill() {
		if(unsavedChanges == true) {
			switch(pm.yesNoPrompt("Save before closing?")) {
			case SWT.YES:
				if(currentFile != null)
					save(currentFile.getAbsolutePath());
				else
					saveAs();
				display.dispose();
				break;
			case SWT.NO:
				display.dispose();
				break;
			case SWT.CANCEL:
				break;
			}
		} else {
			display.dispose();
		}	
	}

Open in new window

0
Comment
Question by:nphershm
  • 4
  • 2
6 Comments
 
LVL 7

Expert Comment

by:YourReference
ID: 21835689
http://forum.java.sun.com/thread.jspa?threadID=650850&tstart=0

As for the ALT+F4 I don't believe you can intercept that.
0
 

Author Comment

by:nphershm
ID: 21835730
This first line:
ramka.addWindowListener(new WindowAdapter()

I don't think would work in my application. I imagine ramka is like a JFrame or some swing window, I'm using SWT and as you can see from the current listener my code listens for a shell event (SWT.Close). I don't know where or how I would add a WindowListener to my SWT application.

Does that help?
0
 
LVL 7

Accepted Solution

by:
YourReference earned 50 total points
ID: 21835752
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:nphershm
ID: 21838843
Thanks, I've been trying really hard to get this all configured, I had to chase down some new includes to solve a bunch of ClassNotFoundExceptions. I haven't been able to implement yet the listener that would get my code to the "handleShellCloseEvent()" function, but this has been the best lead yet, thanks... I'll let you know how it goes!

- Nick
0
 

Author Comment

by:nphershm
ID: 21842211
So it turns out that I had to reconfigure my app to use JFrame so that I could take advantage of this solution. MyApp class now extends ApplicationWindow, and when I override the function handleShellCloseEvent() I can capture and trap Alt+F4, and all of the other methods of closing the program. This has enabled me to tidy up the data, allow the user to save any changes and then shut down without any loss of data.

Thanks for the help!

- Nick
0
 

Author Closing Comment

by:nphershm
ID: 31469358
Because I was not implementing JFace prior to this solution I had to go do a complete re-working of MyApp to extend the JFace class ApplicationWindow. Once the main MyApp class extends ApplicationWindow I was able to override the handleShellCloseEvent() method that the first link you sent suggests. This was a fine solution, I just had to go do some hours of tinkering with JFace in order to implement it. Thank you for the help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A list of useful business intelligence software.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
Viewers will learn how to use the Hootsuite Dashboard.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question