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 setDontSaveButton(final JRootPane rp, final JButton b) {
    rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "dontSave");
    rp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
            .put(KeyStroke.getKeyStroke(KeyEvent.VK_D, rp.getToolkit().getMenuShortcutKeyMask()), "dontSave");
    rp.getActionMap().put("dontSave", new AbstractAction() {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent ev) {
            b.doClick();/*ww w.  ja v  a 2s  .c om*/
        }
    });
}

From source file:EscapeDialog.java

License:asdf

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("about to disappear");
            setVisible(false);//  w w  w .  jav  a2 s . co m
        }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
}

From source file:Main.java

/**
 * Registers the keystroke of the given action as "command" of the given
 * component./*from  www . ja v  a2 s  .  c  o m*/
 * <p>
 * This code is based on the Sulky-tools, found at
 * &lt;http://github.com/huxi/sulky&gt;.
 * </p>
 * 
 * @param aComponent
 *          the component that should react on the keystroke, cannot be
 *          <code>null</code>;
 * @param aAction
 *          the action of the keystroke, cannot be <code>null</code>;
 * @param aCommandName
 *          the name of the command to register the keystore under.
 */
public static void registerKeystroke(final JComponent aComponent, final Action aAction,
        final String aCommandName) {
    final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY);
    if (keyStroke == null) {
        return;
    }

    InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = aComponent.getActionMap();
    inputMap.put(keyStroke, aCommandName);
    actionMap.put(aCommandName, aAction);

    inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED);
    Object value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }

    inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    value = inputMap.get(keyStroke);
    if (value != null) {
        inputMap.put(keyStroke, aCommandName);
    }
}

From source file:Main.java

/**
 * Sets the hot key for focus.// w w w.  j a v  a2s. co m
 *
 * @param comp
 *            the comp
 * @param keyStroke
 *            the key stroke
 * @param actionName
 *            the action name
 */
public static void setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName) {
    // get the button's Action map
    final ActionMap amap = comp.getActionMap();
    // add an action to the button's action map
    // and give it a name(it can be any object not just String)
    amap.put(actionName, new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent e) {
            // call your a method that contains your action code
            comp.requestFocus();
        }
    });
    // get the input map for the button
    final InputMap imap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    // add a key stroke associated with an action in the action map(action
    // name).
    // imap.put(KeyStroke.getKeyStroke("F1"),"ActionName");
    // you can do the same for more than one key.
    imap.put(KeyStroke.getKeyStroke(keyStroke), actionName);
}

From source file:Main.java

private void addKeyBind(JComponent contentPane, String key) {
    InputMap iMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap aMap = contentPane.getActionMap();
    iMap.put(KeyStroke.getKeyStroke(key), DISABLE_CLICKER);
    aMap.put(DISABLE_CLICKER, disableButtonAction);
}

From source file:EscapeDialog.java

protected JRootPane createRootPane() {
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);/*from   w w  w. j a  va2  s .  c  o m*/
        }
    };
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    rootPane.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
    return rootPane;
}

From source file:TDialog.java

protected JRootPane createRootPane() {
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            hide();//w w  w  . j  av a  2s.c  o m
            bCancel = true;
        }
    };

    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
    JRootPane rootPane = super.createRootPane();

    rootPane.registerKeyboardAction(al, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

    return rootPane;
}

From source file:com.mirth.connect.manager.HeapSizeDialog.java

public HeapSizeDialog(String heapSize) {
    super(PlatformUI.MANAGER_DIALOG, true);

    managerController = ManagerController.getInstance();
    getRootPane().registerKeyboardAction(new ActionListener() {
        @Override/*  ww w .j ava 2s.  c  o  m*/
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);

    setResizable(false);
    setBackground(Color.white);
    setTitle("Web Start Settings");
    getContentPane().setBackground(Color.white);

    initComponents();

    this.heapSize = StringUtils.isEmpty(heapSize) ? "512m" : heapSize;

    String heapSizeOption = HeapSize.toDisplayName(heapSize);
    if (StringUtils.isBlank(heapSizeOption)) {
        heapSizeOption = this.heapSize;
    }

    // Add any non-default properties to the model
    String property = (String) managerController.getServerProperties()
            .getProperty(ManagerConstants.ADMINISTRATOR_MAX_HEAP_SIZE);
    if (!heapSizeComboboxModel.contains(property)
            && !heapSizeComboboxModel.contains(HeapSize.toDisplayName(property))) {
        heapSizeComboboxModel.add(formatCustomProperty(property));
    }

    // Resort list by sizes
    List<String> mbList = new ArrayList<String>();
    List<String> gbList = new ArrayList<String>();
    for (String size : heapSizeComboboxModel) {
        if (size.contains("M")) {
            mbList.add(size);
        } else {
            gbList.add(size);
        }
    }

    Collections.sort(mbList);
    Collections.sort(gbList);
    mbList.addAll(gbList);

    heapSizeComboBox = new JComboBox(mbList.toArray());
    heapSizeComboBox.getModel().setSelectedItem(formatCustomProperty(heapSizeOption));

    initLayout();
    pack();
    setLocationRelativeTo(PlatformUI.MANAGER_DIALOG);
    setVisible(true);
}

From source file:com.mirth.connect.client.ui.ChannelTagDialog.java

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();

    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    rootPane.registerKeyboardAction(new ActionListener() {
        @Override// w  ww  .  j av a 2s  . c o  m
        public void actionPerformed(ActionEvent e) {
            addButtonActionPerformed(null);
        }
    }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);

    return rootPane;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFrameActionsHolder.java

public void addAction(Action action, int index) {
    int oldIndex = findActionById(actionList, action.getId());
    if (oldIndex >= 0) {
        removeAction(actionList.get(oldIndex));
        if (index > oldIndex) {
            index--;// w w w. j  a  v  a 2  s  .c om
        }
    }

    if (action.getShortcutCombination() != null) {
        KeyStroke keyStroke = DesktopComponentsHelper.convertKeyCombination(action.getShortcutCombination());
        InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(keyStroke, action.getId());
        ActionMap actionMap = panel.getActionMap();
        actionMap.put(action.getId(), new ValidationAwareAction() {
            @Override
            public void actionPerformedAfterValidation(ActionEvent e) {
                action.actionPerform(component);
            }
        });
        shortcutActions.put(action, keyStroke);
    }

    actionList.add(index, action);
}