Solved

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

Posted on 2008-06-20
6
522 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:nphershm
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A list of useful business intelligence software.
If your app took Google’s lash recently, here are the 5 most likely reasons.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now