import java.awt.*; import java.awt.event.*; import javax.swing.*; class Test { public static void main(String args[]) { // Set up the JFrame JFrame jf = new JFrame(); jf.setSize(200,200); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // A label and a button JCheckBox cb = new JCheckBox("dummy"); JButton b = new JButton(); // Actions for the button to perform Action focused = new AbstractAction(){ public void actionPerformed (ActionEvent e) { System.out.println ("I'm Focused"); } }; Action inWindow = new AbstractAction(){ public void actionPerformed (ActionEvent e) { System.out.println ("I'm In Focused Window"); } }; // Register the actions with the key 'a' KeyStroke k = KeyStroke.getKeyStroke('a'); b.registerKeyboardAction(focused, "focused", k, JComponent.WHEN_FOCUSED); b.registerKeyboardAction(inWindow, "window", k, JComponent.WHEN_IN_FOCUSED_WINDOW); // Add the components to the JFrame and show it jf.add(cb, BorderLayout.NORTH); jf.add(b, BorderLayout.SOUTH); jf.show(); } }