Abort Windows exit from Java

I have a Swing application where a user can create and save documents. The app is deployed as a simple Jar file.

The main target platform is Windows Vista.

If the user has an open and unsaved document in the application and the user logs off Windows Vista, I would like to stop the logout process and ask the user if he wants to save the document before the Java application is finished.

Can a Java application stop the registration process in Windows Vista?

I have tried the stop hook with no success.

+3


source to share


2 answers


Can a Java application stop the registration process in Windows Vista?



No, and it shouldn't be possible. It's like a tail wagging a dog.

+4


source


EDIT2


For those following the discussion, I'm leaving the first answers I have, but they don't seem to work. Find your real solution first.

Ok, so I think it does work, but it's not entirely acceptable since it used a limited portion of the API (but it has been around since Java 1.3 and is still present in Java 1.7). It uses sun.misc.Signal. Most of the code was originally sent 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 BEFORE PROPOSED (DOES NOT WORK ON LOGOFF)

I am assuming you are using a GUI application with JFrame

.

In yours, JFrame

install the following:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

      



Then register WindowAdapter

at JFrame

. Override the method windowClosing()

and from there open a blocking dialog to ask the user what they want to do (Yes / NO / CANCEL). If he chooses YES, you save and then delete the frame, in case he chooses NO, you just delete the frame. If he chooses to cancel, you don't do anything.


EDIT:

Here are some codes and more details on what I am explaining and which were contributed by Andrew Thompson . All credits must go to him for:

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 for Windows to shut down, I see ..

Windows refusing to shut-down

Even more interesting, after I clicked Cancel(heck, "Search for UFOs" was the next track in the queue in the player, and I was not going to reschedule it :), I could not click on the frame. It seemed like it was being blocked by an invisible modal dialog. I had to kill the virtual machine to get rid of it.

+2


source







All Articles