Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why will this KeyListener not work?

 
Hans vogn
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell me where i can learn how to use keybindings?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll try.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic