calling actionPerformed
i have a JButton implemented actionPerformed. i am want to fire the same if press F1.how can i call the actionPerformed()
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher ( new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID()==KeyEvent.KEY_PRESSED && e.getKeyCode() == e.VK_F1 )
{
System.out.println("F1 pressed"); //here i need to call
}
return true;
}
} );
Thanks
> i have a JButton implemented actionPerformed. i am
> want to fire the same if press F1.how can i call
> the actionPerformed()
>
> KeyboardFocusManager.getCurrentKeyboardFocusManager().
> addKeyEventDispatcher ( new KeyEventDispatcher() {
> public boolean dispatchKeyEvent(KeyEvent e)
> Event e) {
> if(e.getID()==KeyEvent.KEY_PRESSED &&
> RESSED && e.getKeyCode() == e.VK_F1 )
> {
button.doClick();
> }
> return true;
> }
> } );
>
Not sure.. might work
I think you should use such code:
final JButton button = new JButton("My Button");
KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
((JComponent)getContentPane()).registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.doClick();
}
}, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
genga at 2007-7-16 1:17:35 >
