Solved

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

Posted on 2008-06-20
6
525 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migration from Netsuite to Quickbooks 1 41
Image decoding from Camera 3 72
custom annotations 9 32
Spring Framework HTTPSession management 1 24
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
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…

867 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

17 Experts available now in Live!

Get 1:1 Help Now