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

[505 byte] By [umamittapallia] at [2007-10-2 5:15:08]
# 1

> 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

watfora at 2007-7-16 1:17:35 > top of Java-index,Desktop,Core GUI APIs...
# 2

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 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks a lot... working
umamittapallia at 2007-7-16 1:17:35 > top of Java-index,Desktop,Core GUI APIs...