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?

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?

How about switching the focus from your JButton to JFrame which will do the trick

Just add this code before setting your JFrame visibility

setFocusable(true);

before your

setVisible(true);

call.

commented: helpful post. thnx! +1

Yes, focus issues can be a pain with key listeners. The InputMap and ActionMap make it easier to handle this. Try using this instead of the KeyListener.

private void launch() {
    getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
      KeyStroke.getKeyStroke("R"), "reset");
    
    getRootPane().getActionMap().put("reset", new AbstractAction() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            System.out.println("reset");
        }
    });

    setBounds(100, 100, 300, 300);

    add(new JButton("hi")); //here is the perp

    setVisible(true);
}
commented: great post! +1

Thanks you two.....

I used setFocusable(true); for the JFrame and setFocusable(false); for the JButtons

Now my JFrame is always in focus and I don't get that little square box that shows up on the buttons anymore... Solved both problems at once. :)

I also tried Ezzaral's method (which i like btw) and it worked perfect as well. Plus, if focusing is important to you, this doesn't affect anything focusable in your program..

This is A+ help.... thnx guys.. +Rep points! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.