Java JFrame Parent installOperation(final RootPaneContainer frame, final int condition, final KeyStroke keyStroke, final String actionKey, Action action)

Here you can find the source of installOperation(final RootPaneContainer frame, final int condition, final KeyStroke keyStroke, final String actionKey, Action action)

Description

Installs a keybind in the specified frame for the specified action.

License

Open Source License

Parameter

Parameter Description
frame The frame in which this keybind can be activated
condition When should this keybind be activated. Either JComponent#WHEN_FOCUSED , JComponent#WHEN_IN_FOCUSED_WINDOW , or JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT .
keyStroke The keystroke used to activate the keybind
actionKey Identifier of the action
action The action to execute when the keybind is activated

Declaration

public static void installOperation(final RootPaneContainer frame, final int condition,
        final KeyStroke keyStroke, final String actionKey, Action action) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;

import javax.swing.JRootPane;
import javax.swing.KeyStroke;
import javax.swing.RootPaneContainer;

public class Main {
    /**/*from w  w  w . j a  va 2 s  . c o  m*/
     * Installs a keybind in the specified frame for the specified action.
     * 
     * @param frame
     *            The frame in which this keybind can be activated
     * @param condition
     *            When should this keybind be activated.
     *            Either {@link JComponent#WHEN_FOCUSED}, {@link JComponent#WHEN_IN_FOCUSED_WINDOW}, or
     *            {@link JComponent#WHEN_ANCESTOR_OF_FOCUSED_COMPONENT}.
     * @param keyStroke
     *            The keystroke used to activate the keybind
     * @param actionKey
     *            Identifier of the action
     * @param action
     *            The action to execute when the keybind is activated
     */
    public static void installOperation(final RootPaneContainer frame, final int condition,
            final KeyStroke keyStroke, final String actionKey, Action action) {
        JRootPane root = frame.getRootPane();
        root.getInputMap(condition).put(keyStroke, actionKey);
        root.getActionMap().put(actionKey, action);
    }

    /**
     * {@linkplain #installOperation(RootPaneContainer, int, KeyStroke, String, Action)}
     */
    public static Action installOperation(final RootPaneContainer frame, final int condition,
            final KeyStroke keyStroke, final String actionKey, final Runnable runnable) {
        Action result = new AbstractAction(actionKey) {
            public void actionPerformed(ActionEvent e) {
                runnable.run();
            }
        };

        installOperation(frame, condition, keyStroke, actionKey, result);
        return result;
    }
}

Related

  1. getRootFrame(Component aComp)
  2. getRootFrame(Component c)
  3. getRootFrame(java.awt.Component c)
  4. getRootFrame(JComponent component)
  5. getSpssInstallationDirectory(Frame parent)
  6. isFrameModified(RootPaneContainer frame)
  7. okToWriteFile(Frame parent, String fileName)
  8. oops(Frame parent, String s)
  9. rootFrame(@Nonnull Component component)