• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

KeyListener not working on some PCs  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andreas Beck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. Here's a compilable code:
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also have java 6, and it doesn't work unless you click the applet to give it focus
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 23689
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!