Java tutorial
//package com.java2s; /* * * This file is part of Genome Artist. * * Genome Artist 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. * * Genome Artist 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 Genome Artist. If not, see <http://www.gnu.org/licenses/>. * */ import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.KeyStroke; public class Main { /** * Obtine actiunea inregistrata pentru apasarea de tasta * @return actiune inregistrata pentru tasta sau null */ public static Action getActionForKeystroke(JComponent component, KeyStroke keyStroke) { Action whenFocused = getActionForKeystroke(component, JComponent.WHEN_FOCUSED, keyStroke); Action whenAncestorOfFocused = getActionForKeystroke(component, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyStroke); Action whenInWindow = getActionForKeystroke(component, JComponent.WHEN_IN_FOCUSED_WINDOW, keyStroke); //Ordinea preferata pentru a le returna if (whenFocused != null) return whenFocused; if (whenAncestorOfFocused != null) return whenAncestorOfFocused; if (whenInWindow != null) return whenInWindow; //Valoare default return null; } /** * Obtine actiunea inregistrata pentru apasarea de tasta * @return */ public static Action getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke) { //Identify the action key InputMap inputMap = component.getInputMap(inputMapId); String key = (String) inputMap.get(keyStroke); //Get the action if (key != null) { ActionMap localMap = component.getActionMap(); return localMap.get(key); } else { return null; } } }