Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.event.KeyEvent;

import javax.swing.ComponentInputMap;

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

import javax.swing.KeyStroke;

public class Main {
    public static void updateToggleMnemonic(JComponent c, int oldKey, int newKey) {
        if (oldKey == newKey) {
            return;
        }

        InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        if (map != null && oldKey != 0) {
            map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK, false));
            map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK, true));
            map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.VK_UNDEFINED, true));
        }
        if (newKey != 0) {
            if (map == null) {
                map = new ComponentInputMap(c);
                c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map);
            }
            map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK, false), "press");
            map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK, true), "release");
            map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.VK_UNDEFINED, true), "release");
        }
    }
}