my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes KeyListener not working on some PCs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener not working on some PCs" Watch "KeyListener not working on some PCs" New topic
Author

KeyListener not working on some PCs

Andreas Beck
Greenhorn

Joined: Aug 02, 2012
Posts: 7
Hi everyone!
I'm currently writing a JApplet that has to respond to Key Events and Mouse Events. So I implemented a KeyListener and a MouseListener and wrote the method

When I run the program and press a key on the keyboard, the message pops up. When a friend of mine runs the program, nothing happens.
Mouse Events work on my and my friend's pc...

Any suggestions? Maybe it's because I have Java 6 and he has Java 7?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Insufficient information, What component is the KeyListener added to? Swing is designed to use key bindings, why use a KeyListener? Does your code respect Swing's single threaded rule?

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.


luck, db
There are no new questions, but there may be new answers.
Andreas Beck
Greenhorn

Joined: Aug 02, 2012
Posts: 7
I'm sorry. Here's a compilable code:
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I also have java 6, and it doesn't work unless you click the applet to give it focus
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

My recommendation to use key bindings stands, but if for some silly reason you have to use a key listener, setFocusable(true) and maybe requestFocusInWindow() of the JApplet's contentPane., Should work.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Michael Dunn wrote:I also have java 6, and it doesn't work unless you click the applet to give it focus


This was because Microsoft lost a lawsuit and they had to make Internet Explorer work that way, I think. Maybe the problem is browser-dependent?
Andreas Beck
Greenhorn

Joined: Aug 02, 2012
Posts: 7
Thanks, I finally got the time to test it. setFocusable(true) and requestFocusInWindow() did the trick. For now it works, but sooner or later I'll try using the key bindings.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: KeyListener not working on some PCs