-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExitAction.java
38 lines (34 loc) · 1.18 KB
/
ExitAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.KeyboardFocusManager;
import java.awt.Window;
import javax.swing.Action;
import javax.swing.AbstractAction;
/**
* This class will create and dispatch a WINDOW_CLOSING event to the active
* window. As a result any WindowListener that handles the windowClosing
* event will be executed. Since clicking on the "Close" button of the
* frame/dialog or selecting the "Close" option from the system menu also
* invoke the WindowListener, this will provide a commen exit point for
* the application.
*/
public class ExitAction extends AbstractAction
{
public ExitAction()
{
super("Exit");
putValue( Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_X) );
}
@Override
public void actionPerformed(ActionEvent e)
{
// Find the active window before creating and dispatching the event
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
if (window != null)
{
WindowEvent windowClosing = new WindowEvent(window, WindowEvent.WINDOW_CLOSING);
window.dispatchEvent(windowClosing);
}
}
}