943,949 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 10598
  • Java RSS
May 23rd, 2008
0

extends JFrame implements KeyListener

Expand 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


Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
May 23rd, 2008
1

Re: extends JFrame implements KeyListener

Click to Expand / Collapse  Quote originally posted by TheWhite ...
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)
  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
Java Syntax (Toggle Plain Text)
  1. setFocusable(true);
before your
Java Syntax (Toggle Plain Text)
  1. setVisible(true);
call.
Reputation Points: 11
Solved Threads: 1
Newbie Poster
cbalu is offline Offline
6 posts
since Sep 2007
May 23rd, 2008
1

Re: extends JFrame implements KeyListener

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)
  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. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 23rd, 2008
0

Re: extends JFrame implements KeyListener

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!
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: ;expected error
Next Thread in Java Forum Timeline: Help On how to set time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC