aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Not getting key pressed event. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Not getting key pressed event." Watch "Not getting key pressed event." New topic
Author

Not getting key pressed event.

Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
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.


Your help will be greatly appreciated,
Alejandro Barrero
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Shouldn't you be using

instead of


[My Blog]
All roads lead to JavaRanch
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

I didn't suggest to remove it, but to replace it with "addKeyListener(this);"
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Key bindings are recommended over using a key listener.
http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html


luck, db
There are no new questions, but there may be new answers.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
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

Joined: Aug 01, 2005
Posts: 319
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

Joined: Jun 13, 2009
Posts: 2200
    
    7
The code you posted is NOT a SSCCE.
Alejandro Barrero
Ranch Hand

Joined: Aug 01, 2005
Posts: 319
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

Joined: Nov 19, 2008
Posts: 8
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

Joined: Aug 01, 2005
Posts: 319
Thank you Peter; I understand now that the code I post should be runnable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not getting key pressed event.