2

I have a java program with a JFrame and 3 JButtons in it. I have added a keylistener to jframe. When i run the program a jframe window is opened and the first button is selected by default. My problem is that a KeyEvent is not being generated by this JFrame. Now, besides adding a KeyListener to the jframe, i also added a KeyListener to the buttons. Now the keyevent is being generated by the buttons.

How do I make the JFrame generate KeyEvent instead of the JButton generating them ??

Actually, my main purpose is building keyboard shortcuts for the buttons.

awareeye
  • 3,051
  • 3
  • 22
  • 22

2 Answers2

4

The key event is called on the currently focused component (which is usually not the JFrame)

MByD
  • 135,866
  • 28
  • 264
  • 277
  • so is there a way to generate keyevent from jframe even when it is not focused?? – awareeye Aug 03 '11 at 05:58
  • Not really, you will have to register a listener to any relevant component, on the bright side - you can register the same listener :) see here: http://stackoverflow.com/questions/286727/java-keylistener-for-jframe-is-being-unresponsive – MByD Aug 03 '11 at 06:00
  • Yes, use Key Bindings, not KeyEvents. Swing was designed to use Key Bindings. – camickr Aug 03 '11 at 15:03
4

Have a look here How to Use Key Bindings.
An alternative to keylistener.

Here is a little Example it has a Button with focus and process a KeyEvent (F2).
On F2-clicked the Key-Binding process a ButtonClick which performed a System.out print.

public class Example {
    static public void main( String[] s ) {
        EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.getContentPane().setLayout( new BorderLayout() );
                frame.setBounds( 50, 50, 600, 600 );
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                final JButton button = new JButton( new AbstractAction("MyButton") {
                    @Override
                    public void actionPerformed( ActionEvent e ) {
                        System.out.println("Button Clicked");
                    }
                });
                frame.getContentPane().add( button );
                frame.getRootPane().setDefaultButton( button );

                KeyStroke f2 = KeyStroke.getKeyStroke("F2");
                frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(f2, "clickButton");
                frame.getRootPane().getActionMap().put("clickButton", new AbstractAction() {
                    @Override
                    public void actionPerformed( ActionEvent e ) {
                        button.doClick();
                    }
                });
                frame.setVisible( true );
                // the Button has the focus
                button.requestFocus();
                // generate a KeyEvent 'F2' 
                KeyboardFocusManager.getCurrentKeyboardFocusManager().dispatchKeyEvent( new KeyEvent( frame, KeyEvent.KEY_PRESSED, 0, f2.getModifiers(), f2.getKeyCode(), f2.getKeyChar() ) );
            }
        });
    }
}
oliholz
  • 7,447
  • 2
  • 43
  • 82
  • 1
    thanks for ur answer... but can u please explain what the getInputMap() method and put() methods do.. Any help will be greatly appreciated – awareeye Aug 03 '11 at 10:42
  • +1, Swing was designed to use Key Bindings. The tutorial explains those methods. If we don't know what you find confusing about the tutorial explanation we can't provide additional help. Maybe my little [Key Bindings](http://tips4java.wordpress.com/2008/10/10/key-bindings/) utility will help you better understand which InputMap to use. – camickr Aug 03 '11 at 15:07