extends JFrame implements KeyListener

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

extends JFrame implements KeyListener

 
0
  #1
May 23rd, 2008
I'm trying to add a KeyListener to my JFrame, but it doesn't seem to work when I have a JButton floating around


  1. import java.awt.event.KeyEvent;
  2. import java.awt.event.KeyListener;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.SwingUtilities;
  6.  
  7.  
  8. public class Testing extends JFrame implements KeyListener{
  9.  
  10. public static void main(String[] args){
  11. SwingUtilities.invokeLater(new Runnable() {
  12. public void run(){
  13. new Testing();
  14. }
  15. });
  16. }
  17.  
  18. private Testing(){
  19. super("test");
  20. launch();
  21. }
  22.  
  23. private void launch(){
  24. addKeyListener(this);
  25. setBounds(100,100,300,300);
  26.  
  27. //add(new JButton("hi")); //here is the perp
  28.  
  29. setVisible(true);
  30. }
  31.  
  32. public void keyPressed(KeyEvent evt){
  33. }
  34. public void keyReleased(KeyEvent evt){
  35. char ch = evt.getKeyChar();
  36. switch(ch){
  37. case 'r': System.out.println("reset"); break;
  38. }
  39. }
  40. public void keyTyped(KeyEvent evt) {
  41. }
  42.  
  43. }

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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: cbalu is an unknown quantity at this point 
Solved Threads: 1
cbalu cbalu is offline Offline
Newbie Poster

Re: extends JFrame implements KeyListener

 
1
  #2
May 23rd, 2008
Originally Posted by TheWhite View Post
I'm trying to add a KeyListener to my JFrame, but it doesn't seem to work when I have a JButton floating around


  1. import java.awt.event.KeyEvent;
  2. import java.awt.event.KeyListener;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.SwingUtilities;
  6.  
  7.  
  8. public class Testing extends JFrame implements KeyListener{
  9.  
  10. public static void main(String[] args){
  11. SwingUtilities.invokeLater(new Runnable() {
  12. public void run(){
  13. new Testing();
  14. }
  15. });
  16. }
  17.  
  18. private Testing(){
  19. super("test");
  20. launch();
  21. }
  22.  
  23. private void launch(){
  24. addKeyListener(this);
  25. setBounds(100,100,300,300);
  26.  
  27. //add(new JButton("hi")); //here is the perp
  28.  
  29. setVisible(true);
  30. }
  31.  
  32. public void keyPressed(KeyEvent evt){
  33. }
  34. public void keyReleased(KeyEvent evt){
  35. char ch = evt.getKeyChar();
  36. switch(ch){
  37. case 'r': System.out.println("reset"); break;
  38. }
  39. }
  40. public void keyTyped(KeyEvent evt) {
  41. }
  42.  
  43. }

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
  1. setFocusable(true);
before your
  1. setVisible(true);
call.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: extends JFrame implements KeyListener

 
1
  #3
May 23rd, 2008
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.
  1. private void launch() {
  2. getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
  3. KeyStroke.getKeyStroke("R"), "reset");
  4.  
  5. getRootPane().getActionMap().put("reset", new AbstractAction() {
  6. public void actionPerformed(java.awt.event.ActionEvent e) {
  7. System.out.println("reset");
  8. }
  9. });
  10.  
  11. setBounds(100, 100, 300, 300);
  12.  
  13. add(new JButton("hi")); //here is the perp
  14.  
  15. setVisible(true);
  16. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

Re: extends JFrame implements KeyListener

 
0
  #4
May 23rd, 2008
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC