Java Swing KeyStroke registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)

Here you can find the source of registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener, final String actionCommand)

Description

Registers a keyboard handler to a given component

License

Open Source License

Parameter

Parameter Description
component Component to register the handler to
stroke Keystroke to activate the handler
listener ActionListener to handle the key press
actionCommand Action command string

Declaration

public static void registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener,
        final String actionCommand) 

Method Source Code

//package com.java2s;
/*/*from w w w. j a  va 2s . c o  m*/
 * Copyright 2006-2015 The MZmine 2 Development Team
 * 
 * This file is part of MZmine 2.
 * 
 * MZmine 2 is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * MZmine 2 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * MZmine 2; if not, write to the Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA 02110-1301 USA
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;

import javax.swing.JComponent;

import javax.swing.KeyStroke;

public class Main {
    /**
     * Registers a keyboard handler to a given component
     * 
     * @param component
     *            Component to register the handler to
     * @param stroke
     *            Keystroke to activate the handler
     * @param listener
     *            ActionListener to handle the key press
     * @param actionCommand
     *            Action command string
     */
    public static void registerKeyHandler(JComponent component, KeyStroke stroke, final ActionListener listener,
            final String actionCommand) {
        component.getInputMap().put(stroke, actionCommand);
        component.getActionMap().put(actionCommand, new AbstractAction() {

            /**
            * 
            */
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent event) {
                ActionEvent newEvent = new ActionEvent(event.getSource(), ActionEvent.ACTION_PERFORMED,
                        actionCommand);
                listener.actionPerformed(newEvent);
            }
        });
    }
}

Related

  1. registerAction(JComponent comp, Action action, String key)
  2. registerAction(JComponent component, int condition, KeyStroke key, Action action, String command)
  3. registerAsAction(final KeyStroke keyStroke, final String actionKey, final Runnable action, final JComponent component)
  4. registerKeyBinding(final JComponent aComponent, final String aKeyStroke, final Action aAction)
  5. registerKeyBoardAction(JComponent comp, Action action, KeyStroke stroke)
  6. registerKeystroke(final JComponent aComponent, final Action aAction, final String aCommandName)
  7. registerTabKey(Container container)
  8. removeAcceleratorFromChildren(Container container, KeyStroke accelerator)
  9. removeAcceleratorFromComponent(JComponent component, KeyStroke accelerator)