I'm trying to add a KeyListener to my JFrame, but it doesn't seem to work when I have a JButton floating around
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Testing extends JFrame implements KeyListener{
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
new Testing();
}
});
}
private Testing(){
super("test");
launch();
}
private void launch(){
addKeyListener(this);
setBounds(100,100,300,300);
//add(new JButton("hi")); //here is the perp
setVisible(true);
}
public void keyPressed(KeyEvent evt){
}
public void keyReleased(KeyEvent evt){
char ch = evt.getKeyChar();
switch(ch){
case 'r': System.out.println("reset"); break;
}
}
public void keyTyped(KeyEvent evt) {
}
}
pressing 'r' should show "reset" in the console
try removing the comment "//" tag from this line: //add(new JButton("hi"));
now pressing 'r' stops showing "reset"......
Now, I understand adding the KeyListener to the JButton would solve my problem in this tiny case, but if I have multiple JButtons hanging around (not just 1), I'd have to add the KeyListener to every single JButton in case either one of them got the focus last... I'm just not keen to this idea...
---I'm trying to get the entire window to listen for when I press 'r', not just the JButton (s)...
I always thought
"addKeyListener(this);" would bind the listening to the entire JFrame, but it seems to loose focus if a JButton is around..
Any suggestions?