Binds an Action to a JComponent via the Action's configured ACCELERATOR_KEY. - Java Swing

Java examples for Swing:JComponent

Description

Binds an Action to a JComponent via the Action's configured ACCELERATOR_KEY.

Demo Code


//package com.java2s;

import javax.swing.*;

public class Main {
    /**/*from w w  w.j a  v  a  2 s. c om*/
     * Binds an Action to a JComponent via the Action's configured ACCELERATOR_KEY.
     */
    public static void initKeyBinding(JComponent component, Action action) {
        KeyStroke keyStroke = (KeyStroke) action
                .getValue(Action.ACCELERATOR_KEY);
        initKeyBinding(component, keyStroke, action);
    }

    /**
     * Binds an Action to a JComponent via the given KeyStroke.
     */
    public static void initKeyBinding(JComponent component,
            KeyStroke keyStroke, Action action) {
        String name = (String) action.getValue(Action.NAME);
        component.getActionMap().put(name, action);
        component.getInputMap().put(keyStroke, name);
    }
}

Related Tutorials