Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-20
6
Medium Priority
?
539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up 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.

 

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

704 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