Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not getting key pressed event.

 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My class is very simple:

I have this panel as the only component in a JFrame in MyEclipse and the debugger doesn't break at
.
I am creating the class with MyEclipse Matisse but I don't think that is a problem (I also tried to create the listener with Matisse but it didn't work either). It is so simple that I don't understand what I am doing wrong.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't you be using

instead of
 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Christophe. That piece of code

was a hold over from when I was creating the listener with Matisse; bu after I delete it I still have the same problem.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't suggest to remove it, but to replace it with "addKeyListener(this);"
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Key bindings are recommended over using a key listener.
http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html
 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Darryll. I followed your suggestion:

I declared

and set

I did my best, although the documentation is not very clear, but still the debugger doesn't break in method keyTypedAction. Also, it appear that with this type of binding I can only detect a specific key but I want an action that detects all printable keys.
 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct Christophe; if the class implements KeyListener, it should set itself as a listener. However, I followed your suggestion butstill the debugger doesn't break in the listener method:
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you posted is NOT a SSCCE.
 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your guidance Rob. I thought I was following the accepted definition, but it appears I need to learn more; please give me details as to what I am doing wrong.
 
Peter Kolb
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alejandro,
if people have less work in getting your code to run, they are generally more willing to try running it.
Your class is missing the public static main(String[]) method and is thus not directly runnable.
Having said that: in order to receive key inputs, a component has to have the focus.
Add this method to your SSCEPanel

 
Alejandro Barrero
Ranch Hand
Posts: 355
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Peter; I understand now that the code I post should be runnable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic