Reputation: 41
I have an Swing application in which the user can create and save documents. The application is deployed as a simple Jar-file.
The primary target platform is Windows Vista.
If the user has on opened and non-saved document in the application and the user is logging out from the Windows Vista machine I would like to halt the logging off process and ask the user if he wants to save to document before the Java application is terminated.
Is it possible to halt the logging off process on Windows Vista from an Java application?
I've tried the shutdown hook with no success.
Upvotes: 4
Views: 2187
Reputation: 47637
EDIT2
For those who followed the discussion I leave the first answers I had but it seems like they don't work. First find my real solution.
Ok, so I think this actually works but it is not totally acceptable as it used restricted part of the API (but it exists since Java 1.3 and is still present in Java 1.7). It uses sun.misc.Signal. Most of the code has been originally posted by Andrew Thompson.
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import sun.misc.Signal;
import sun.misc.SignalHandler;
class TestShutDown {
static final String WINDOW_MODIFIED = "windowModified";
TestShutDown() {
final JFrame f = new JFrame("Log Off!");
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.err.println("Window closing");
handleQuit(f);
}
});
Signal.handle(new Signal("TERM"), new SignalHandler() {
@Override
public void handle(Signal arg0) {
handleQuit(f);
}
});
// bad practice, but not the point..
f.setSize(400, 200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
protected static void handleQuit(final JFrame f) {
int result = JOptionPane.showConfirmDialog(f, "Close Me");
if (result == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
public static void main(String[] args) {
// start the GUI on the EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestShutDown();
}
});
}
}
SOLUTION EARLIER SUGGESTED (NOT WORKING ON LOGOFF)
I am assuming that you are using a GUI application with a JFrame
.
In your JFrame
, set the following:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Then, register a WindowAdapter
on your JFrame
. Override the windowClosing()
method and from there open a blocking dialog to ask the user what he wants to do (Yes/NO/CANCEL). In case he chooses, YES, you save and then dispose the frame, in case he chooses NO, you just dispose the frame. If he chooses cancel, you don't do anything.
EDIT:
Here is some code and more details on what I was explaining and that have been brought by Andrew Thompson. All credits should go to him for the following:
Using this code:
import java.awt.event.*;
import javax.swing.*;
class TestShutDown {
TestShutDown() {
final JFrame f = new JFrame("Log Off!");
f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
f.addWindowListener( new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
int result = JOptionPane.showConfirmDialog(f, "Close Me");
if (result==JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
// bad practice, but not the point..
f.setSize(400,200);
f.setLocationByPlatform(true);
f.setVisible(true);
}
public static void main(String[] args) {
// start the GUI on the EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestShutDown();
}
});
}
}
Then telling Windows to shut down, I see..
Even more interestingly, after I hit Cancel ( heck, 'Searching for UFOs' was the next queued track in the player, and I was not about to reschedule it :), I could not click on the frame. It seemed as though it was blocked with an invisible modal dialog. I had to kill the VM to be rid of it.
Upvotes: 3
Reputation: 168845
Is it possible to halt the logging off process on Windows Vista from an Java application?
No, nor should it be possible. That's like the tail wagging the dog.
Upvotes: 3