Example usage for javax.swing InputMap clear

List of usage examples for javax.swing InputMap clear

Introduction

In this page you can find the example usage for javax.swing InputMap clear.

Prototype

public void clear() 

Source Link

Document

Removes all the mappings from this InputMap .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();

    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");

    JButton component = new JButton("button");

    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);

    inputMap.clear();

}

From source file:Main.java

/**
 * A helper for creating and updating key bindings for components with
 * mnemonics. The {@code pressed} action will be invoked when the mnemonic
 * is activated and the {@code released} action will be invoked when the
 * mnemonic is deactivated./*from  w w w .  j  a v  a2s  .com*/
 * <p>
 * TODO establish an interface for the mnemonic properties, such as {@code
 * MnemonicEnabled} and change signature to {@code public static <T extends
 * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String
 * pressed, String released)}
 * 
 * @param c
 *            the component bindings to update
 * @param pressed
 *            the name of the action in the action map to invoke when the
 *            mnemonic is pressed
 * @param released
 *            the name of the action in the action map to invoke when the
 *            mnemonic is released (if the action is a toggle style, then
 *            this parameter should be {@code null})
 * @throws NullPointerException
 *             if the component is {@code null}
 */
public static void updateMnemonicBinding(JComponent c, String pressed, String released) {
    Class<?> clazz = c.getClass();
    int m = -1;

    try {
        Method mtd = clazz.getMethod("getMnemonic");
        m = (Integer) mtd.invoke(c);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to access mnemonic", e);
    }

    InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW);

    if (m != 0) {
        if (map == null) {
            map = new ComponentInputMapUIResource(c);
            SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }

        map.clear();

        //TODO is ALT_MASK right for all platforms?
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed);
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released);
        map.put(KeyStroke.getKeyStroke(m, 0, true), released);
    } else {
        if (map != null) {
            map.clear();
        }
    }
}

From source file:org.dishevelled.brainstorm.BrainStorm.java

/**
 * Initialize components.//from  w  ww .java 2  s .c  o m
 */
private void initComponents() {
    Font font = new Font(chooseFontName(), Font.PLAIN, fontSize);
    hiddenCursor = createHiddenCursor();
    textArea = new JTextArea() {
        /** {@inheritDoc} */
        protected void paintComponent(final Graphics graphics) {
            Graphics2D g2 = (Graphics2D) graphics;
            g2.setRenderingHint(KEY_FRACTIONALMETRICS, VALUE_FRACTIONALMETRICS_ON);
            g2.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            super.paintComponent(g2);
        }
    };
    textArea.setFont(font);
    textArea.setOpaque(true);
    textArea.setCursor(hiddenCursor);
    textArea.setBackground(backgroundColor);
    textArea.setForeground(textColor);
    textArea.setRows(rows);
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);

    // clear all input mappings
    InputMap inputMap = textArea.getInputMap();
    while (inputMap != null) {
        inputMap.clear();
        inputMap = inputMap.getParent();
    }
    // re-add select default input mappings
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "insert-break");
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "insert-tab");
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "delete-previous");
    int keyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, keyMask), "delete-previous-word");

    // add new input mappings
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, keyMask), "increase-font-size");
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, keyMask), "decrease-font-size");
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S, keyMask), "save");
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "quit");
    textArea.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, keyMask), "quit");

    Action increaseFontSizeAction = new IncreaseFontSizeAction();
    Action decreaseFontSizeAction = new DecreaseFontSizeAction();
    Action saveAction = new SaveAction();
    Action quitAction = new QuitAction();

    textArea.getActionMap().put("increase-font-size", increaseFontSizeAction);
    textArea.getActionMap().put("decrease-font-size", decreaseFontSizeAction);
    textArea.getActionMap().put("save", saveAction);
    textArea.getActionMap().put("quit", quitAction);

    placeholder = Box.createGlue();
}