Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-20
6
Medium Priority
?
545 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 200 total points
ID: 21835752
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Independent Software Vendors: 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

If something goes wrong with Exchange, your IT resources are in trouble.All Exchange server migration processes are not designed to be identical and though migrating email from on-premises Exchange mailbox to Cloud’s Office 365 is relatively simple…
Mailbox Corruption is a nightmare every Exchange DBA wishes he never has. Recovering from it can be super-hectic if not entirely futile. And though techniques like the New-MailboxRepairRequest cmdlet have been designed to help with fixing minor corr…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Suggested Courses

782 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