print Input Map for Swing JComponent - Java Swing

Java examples for Swing:JComponent

Description

print Input Map for Swing JComponent

Demo Code

/*//from   w  w w.  j a  v a 2s  .com
 * MiscUtils.java
 *
 * Copyright (C) 2002-2013 Takis Diakoumis
 *
 * 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 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/>.
 *
 */
//package com.java2s;

import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class Main {
    public static void printInputMap(JComponent component) {
        printInputMap(component.getInputMap(JComponent.WHEN_FOCUSED),
                "Input map used when focused");
        printInputMap(
                component
                        .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT),
                "Input map used when ancestor of focused component");
        printInputMap(
                component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW),
                "Input map used when in focused window");
    }

    public static void printInputMap(InputMap inputMap, String heading) {
        System.out.println("\n" + heading + ":");
        KeyStroke[] keys = inputMap.allKeys();
        if (keys != null) {
            for (int i = 0; i < keys.length; i++) {
                KeyStroke key = keys[i];
                Object actionName = inputMap.get(key);
                System.out.println("\tKey: <" + key + ">, action name: "
                        + actionName);
            }
        }
    }
}

Related Tutorials