Example usage for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW

List of usage examples for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW

Introduction

In this page you can find the example usage for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW.

Prototype

int WHEN_IN_FOCUSED_WINDOW

To view the source code for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW.

Click Source Link

Document

Constant used for 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.

Usage

From source file:Main.java

public static void registerKeyBoardAction(JComponent comp, Action action) {
    registerKeyBoardAction(comp, action, JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:Main.java

public static void setKeyAction_WhenInFocusedWindow(JComponent component, int keyCode, String actionIdString,
        Action action) {/*from w  ww  .j  a  v  a2s.c o  m*/
    component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyCode, 0),
            actionIdString);
    component.getActionMap().put(actionIdString, action);
}

From source file:Main.java

public static void updateToggleMnemonic(JComponent c, int oldKey, int newKey) {
    if (oldKey == newKey) {
        return;//from  w w w .  j  a v a 2  s. co  m
    }

    InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (map != null && oldKey != 0) {
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK, false));
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK, true));
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.VK_UNDEFINED, true));
    }
    if (newKey != 0) {
        if (map == null) {
            map = new ComponentInputMap(c);
            c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK, false), "press");
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK, true), "release");
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.VK_UNDEFINED, true), "release");
    }
}

From source file:Main.java

public static void registerKeyBoardAction(JComponent comp, Action action, KeyStroke stroke) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, action.getValue(Action.NAME));
    comp.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void updateMnemonic(JComponent c, int oldKey, int newKey) {
    if (oldKey == newKey) {
        return;/*from   w  ww  .  j ava2 s .c  o  m*/
    }

    InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (map != null && oldKey != 0) {
        map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK));
    }
    if (newKey != 0) {
        if (map == null) {
            map = new ComponentInputMap(c);
            c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }
        map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK), "press");
    }
}

From source file:Main.java

/**
 * Adds a key listener to a given {@link JDialog} that diposes it when the escape
 * key is pressed.//from  w ww.j a v a2 s.  c o  m
 */
public static void addEscapeKeyCloseAction(final JDialog dialog) {
    dialog.getRootPane().registerKeyboardAction(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
}

From source file:Main.java

public static void associaTeclaAtalho(JComponent component, Action action, String nomeAcao, String... atalhos) {
    final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap actionMap = component.getActionMap();
    for (String atalho : atalhos) {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(atalho);
        inputMap.put(keyStroke, nomeAcao);
        actionMap.put(nomeAcao, action);
    }/*  w w w.  ja va 2s  .c om*/
}

From source file:Main.java

public static void closeOnEscape(final JFrame frame) {
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            frame.setVisible(false);//from w  w w.j  av  a  2 s . com
            frame.dispose();
        }
    };
    JRootPane rootPane = frame.getRootPane();
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);
}

From source file:Main.java

public static void updateAccelerator(JMenuItem menuItem, KeyStroke oldAccelerator) {
    KeyStroke accelerator = menuItem.getAccelerator();
    if (oldAccelerator != null && oldAccelerator.equals(accelerator)) {
        return;/*from w  w  w.j  a  va2  s.c o m*/
    }

    InputMap map = menuItem.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    if (map != null && oldAccelerator != null) {
        map.remove(oldAccelerator);
    }
    if (accelerator != null) {
        if (map == null) {
            map = new ComponentInputMap(menuItem);
            menuItem.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }
        map.put(accelerator, "click");
    }
}

From source file:Main.java

/**
 * Adds a component action.// ww  w  .  jav  a  2 s . c o m
 *
 * @param component
 *            The compoennt to add the action to
 * @param action
 *            The action to add
 */

public static void addComponentAction(final JComponent component, final Action action) {
    final InputMap imap = component
            .getInputMap(component.isFocusable() ? JComponent.WHEN_FOCUSED : JComponent.WHEN_IN_FOCUSED_WINDOW);
    final ActionMap amap = component.getActionMap();
    final KeyStroke ks = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
    imap.put(ks, action.getValue(Action.NAME));
    amap.put(action.getValue(Action.NAME), action);
}