| | |
extends JFrame implements KeyListener
Thread Solved |
•
•
Join Date: May 2008
Posts: 122
Reputation:
Solved Threads: 5
I'm trying to add a KeyListener to my JFrame, but it doesn't seem to work when I have a JButton floating around
pressing 'r' should show "reset" in the console
try removing the comment "//" tag from this line:
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?
Java Syntax (Toggle Plain Text)
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?
•
•
Join Date: Sep 2007
Posts: 6
Reputation:
Solved Threads: 1
•
•
•
•
I'm trying to add a KeyListener to my JFrame, but it doesn't seem to work when I have a JButton floating around
Java Syntax (Toggle Plain Text)
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?
Just add this code before setting your JFrame visibility
Java Syntax (Toggle Plain Text)
setFocusable(true);
Java Syntax (Toggle Plain Text)
setVisible(true);
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.
Java Syntax (Toggle Plain Text)
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); }
•
•
Join Date: May 2008
Posts: 122
Reputation:
Solved Threads: 5
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!
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!
![]() |
Similar Threads
- adding keylistener (Java)
- How to make scroll the JLabel in java? (Java)
- Linking JButtons to key events through Key Listeners. (Java)
- Fractals and the Chaos Game help. (Java)
- updating a JList (Java)
- how to (Java)
- I lack focus... (Java)
Other Threads in the Java Forum
- Previous Thread: ;expected error
- Next Thread: Help On how to set time
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






