aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Why will this KeyListener not work? 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 "Why will this KeyListener not work?" Watch "Why will this KeyListener not work?" New topic
Author

Why will this KeyListener not work?

Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Hi

I've used a KeyListener before, and now i need it again, but though i did as before, it will not work.

I have a "set-up" method for the gui that contains "frame.addKeyListener(this);" and a keyPressed(KeyEent ev){System.out.println("Now it works");}

I've looked it through over and over again, but i can't see whats wrong, can someone tell me please?

Here is the code for the class if it helps: http://pastebin.com/m46d78ce9

Thanks in advance

(Im not sure if I've placed the post the rigth place, sorry if it's wrong)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Please supply more details, as requested in this FAQ.
I think this thread would sit better on the Swing forum, so I shall move it.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there's a whole bunch of reasons why keylistener will not 'receive' keyEvents when added to the frame.
possible what's in AndenPanel or LomPanel consumes them, but that's just a guess.

best solution is to use keyBindings instead of keyListener.
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Can you tell me where i can learn how to use keybindings?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18658
    
    8

It's extremely likely that one of the first three links Google returns for the keywords "java keybinding tutorial" will be what you need.
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
I've now looked around for some time, but I can't really find anything that I can understand.

I guesse I'll not be very popular for asking this question, but can anyone write a example for using keybindings please?

thanks (and maybe sorry) in advance
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Hans vogn wrote:I've now looked around for some time, but I can't really find anything that I can understand.
I guesse I'll not be very popular for asking this question, but can anyone write a example for using keybindings please?
thanks (and maybe sorry) in advance

I doubt that any of our examples will be better written then that in the Sun Swing tutorials. You'll probably be better off going through this tutorial and the sample code (perhaps again), and then posting any specific questions about the tutorial and / or the code if you still don't understand it.

Best of luck.
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Thanks, I'll try.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why will this KeyListener not work?