Java Swing KeyStroke addButtonClickKeystroke(final AbstractButton b, KeyStroke ks, Object key)

Here you can find the source of addButtonClickKeystroke(final AbstractButton b, KeyStroke ks, Object key)

Description

Adds a KeyStroke to a AbstractButton .

License

Open Source License

Parameter

Parameter Description
b button to add the Keystroke to
ks the KeyStroke to add
key identifies the the associated Action of the KeyStroke inside the buttons ActionMap and InputMap

Declaration

private static void addButtonClickKeystroke(final AbstractButton b, KeyStroke ks, Object key) 

Method Source Code


//package com.java2s;
/*//w  ww. j  a v a2 s.  co  m
 * funCKit - functional Circuit Kit
 * Copyright (C) 2013  Lukas Elsner <open@mindrunner.de>
 * Copyright (C) 2013  Peter Dahlberg <catdog2@tuxzone.org>
 * Copyright (C) 2013  Julian Stier <mail@julian-stier.de>
 * Copyright (C) 2013  Sebastian Vetter <mail@b4sti.eu>
 * Copyright (C) 2013  Thomas Poxrucker <poxrucker_t@web.de>
 * Copyright (C) 2013  Alexander Treml <alex.treml@directbox.com>
 * 
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import javax.swing.AbstractAction;
import javax.swing.AbstractButton;

import javax.swing.JComponent;

import javax.swing.KeyStroke;

import java.awt.event.ActionEvent;

public class Main {
    /**
     * Adds a {@link KeyStroke} to a {@link AbstractButton}. Pressing this
     * keystroke ends up in a call to {@link AbstractButton#doClick()}. Uses the
     * buttons {@link InputMap} for {@link JComponent#WHEN_IN_FOCUSED_WINDOW}
     * 
     * @param b
     *            button to add the Keystroke to
     * @param ks
     *            the {@link KeyStroke} to add
     * @param key
     *            identifies the the associated {@link Action} of the
     *            {@link KeyStroke} inside the buttons {@link ActionMap} and
     *            {@link InputMap}
     */
    private static void addButtonClickKeystroke(final AbstractButton b, KeyStroke ks, Object key) {
        b.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, key);
        b.getActionMap().put(key, new AbstractAction() {

            private static final long serialVersionUID = 2702589216282710389L;

            @Override
            public void actionPerformed(ActionEvent e) {
                b.doClick();
            }
        });
    }

    /**
     * Same as
     * {@link #addButtonClickKeystroke(AbstractButton, KeyStroke, Object)} but
     * generates the key.
     * 
     * @param b
     * @param ks
     * @see #addButtonClickKeystroke(AbstractButton, KeyStroke, Object)
     */
    public static void addButtonClickKeystroke(final AbstractButton b, KeyStroke ks) {
        Object key = new Object();
        addButtonClickKeystroke(b, ks, key);
    }
}

Related

  1. addFocusBackKey(final int button)
  2. addFocusTraversalKey(final JComponent target, final int keyCode, final int modifiers, final int id)
  3. addHotKey(JComponent pane, Action action, KeyStroke key)
  4. addHotKey(JComponent pane, Action action, KeyStroke key)