List of usage examples for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW
int WHEN_IN_FOCUSED_WINDOW
To view the source code for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW.
Click Source Link
registerKeyboardAction
that means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component. From source file:util.ui.UiUtilities.java
/** * Registers the escape key as close key for a component. * * @param component//from w w w . j a v a 2 s.c o m * The component to close on pressing escape key. */ public static void registerForClosing(final WindowClosingIf component) { Action a = new AbstractAction() { private static final long serialVersionUID = 1L; public void actionPerformed(ActionEvent e) { component.close(); } }; if (OperatingSystem.isMacOs()) { // Add MacOS Apple+W for Closing of Dialogs KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_DOWN_MASK); component.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "CLOSE_ON_APPLE_W"); component.getRootPane().getActionMap().put("CLOSE_ON_APPLE_W", a); stroke = KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.META_DOWN_MASK); component.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "CLOSE_COMPLETE_ON_APPLE"); component.getRootPane().getActionMap().put("CLOSE_COMPLETE_ON_APPLE", new AbstractAction() { public void actionPerformed(ActionEvent e) { MainFrame.getInstance().quit(); } }); } KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); component.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "CLOSE_ON_ESCAPE"); component.getRootPane().getActionMap().put("CLOSE_ON_ESCAPE", a); }
From source file:VASSAL.build.GameModule.java
protected GameModule(DataArchive archive) { this.archive = archive; frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { quit();//from w w w .j a va 2s . c o m } }); addKeyStrokeSource(new KeyStrokeSource(frame.getRootPane(), JComponent.WHEN_IN_FOCUSED_WINDOW)); validator = new CompoundValidityChecker(new MandatoryComponent(this, Documentation.class), new MandatoryComponent(this, GlobalOptions.class)); addCommandEncoder(new ChangePropertyCommandEncoder(propsContainer)); }