button focus with key pressed

hi,i want a button to get focus when the user presses a key on the keyboard. Thankz
[97 byte] By [devsua] at [2007-11-27 4:30:18]
# 1
if u have any idea plz reply
devsua at 2007-7-12 9:39:32 > top of Java-index,Desktop,Core GUI APIs...
# 2
> i want a button to get focus when the user presses a key on the keyboard.a specific key? any key?
Michael_Dunna at 2007-7-12 9:39:32 > top of Java-index,Desktop,Core GUI APIs...
# 3
its a specific key because i have several buttons...
devsua at 2007-7-12 9:39:32 > top of Java-index,Desktop,Core GUI APIs...
# 4

have the focus anywhere in the textFields, press F1, and the focus will go to the button

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

JPanel p = new JPanel(new GridLayout(6,1));

for(int x = 0; x < 5; x++) p.add(new JTextField(5));

final JButton btn = new JButton("OK");

p.add(btn);

JFrame f = new JFrame();

f.getContentPane().add(p);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

KeyboardFocusManager.getCurrentKeyboardFocusManager()

.addKeyEventDispatcher(new KeyEventDispatcher(){

public boolean dispatchKeyEvent(KeyEvent ke){

if(ke.getKeyCode() == KeyEvent.VK_F1 && ke.getID() == KeyEvent.KEY_PRESSED)

{

btn.requestFocusInWindow();

}

return false;

}

});

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 9:39:32 > top of Java-index,Desktop,Core GUI APIs...
# 5
thank uuuuuuuuuuuuuuuuuuuuu very much
devsua at 2007-7-12 9:39:32 > top of Java-index,Desktop,Core GUI APIs...