Here you can find the source of setKeyEvent(JComponent widget, int keyEvent, Runnable action)
public static void setKeyEvent(JComponent widget, int keyEvent, Runnable action)
//package com.java2s; //License from project: Open Source License import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JPanel; import javax.swing.KeyStroke; public class Main { public static void setKeyEvent(JComponent widget, int keyEvent, Runnable action) { String inputMapKey = "key_" + keyEvent; widget.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(keyEvent, 0), inputMapKey); widget.getActionMap().put(inputMapKey, new AbstractAction() { @Override/*from ww w .j a v a 2s . c om*/ public void actionPerformed(final ActionEvent e) { action.run(); } }); } }