Java tutorial
//package com.java2s; /* * $Id: SwingXUtilities.java,v 1.11 2009/04/02 20:35:54 kschaefe Exp $ * * Copyright 2008 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import java.awt.event.InputEvent; import java.lang.reflect.Method; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.KeyStroke; import javax.swing.SwingUtilities; import javax.swing.plaf.ComponentInputMapUIResource; public class Main { /** * A helper for creating and updating key bindings for components with * mnemonics. The {@code pressed} action will be invoked when the mnemonic * is activated. * <p> * TODO establish an interface for the mnemonic properties, such as {@code * MnemonicEnabled} and change signature to {@code public static <T extends * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String * pressed)} * * @param c * the component bindings to update * @param pressed * the name of the action in the action map to invoke when the * mnemonic is pressed * @throws NullPointerException * if the component is {@code null} */ public static void updateMnemonicBinding(JComponent c, String pressed) { updateMnemonicBinding(c, pressed, null); } /** * A helper for creating and updating key bindings for components with * mnemonics. The {@code pressed} action will be invoked when the mnemonic * is activated and the {@code released} action will be invoked when the * mnemonic is deactivated. * <p> * TODO establish an interface for the mnemonic properties, such as {@code * MnemonicEnabled} and change signature to {@code public static <T extends * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String * pressed, String released)} * * @param c * the component bindings to update * @param pressed * the name of the action in the action map to invoke when the * mnemonic is pressed * @param released * the name of the action in the action map to invoke when the * mnemonic is released (if the action is a toggle style, then * this parameter should be {@code null}) * @throws NullPointerException * if the component is {@code null} */ public static void updateMnemonicBinding(JComponent c, String pressed, String released) { Class<?> clazz = c.getClass(); int m = -1; try { Method mtd = clazz.getMethod("getMnemonic"); m = (Integer) mtd.invoke(c); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new IllegalArgumentException("unable to access mnemonic", e); } InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW); if (m != 0) { if (map == null) { map = new ComponentInputMapUIResource(c); SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map); } map.clear(); //TODO is ALT_MASK right for all platforms? map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed); map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released); map.put(KeyStroke.getKeyStroke(m, 0, true), released); } else { if (map != null) { map.clear(); } } } }