• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

KeyListener() is sometimes deaf

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This newbie needs some help from the 'old hands'.
Context: The task of the small program is to display a worldmap image and then to read some data files with vector data to be drawn on top of the image. The approach (mostly plagiarized from "Just Java 2") is to:
instantiate a JFrame
instantiate a JPanel
instantiate an ImageIcon with the background map image
instantiate a JLabel using the ImageIcon
add the JLabel to the JPanel
add a KeyListener using KeyAdapter
add the JPanel to the JFrame (whew!)
The problem is that the KeyListener is sometimes deaf (doesn't respond to keystrokes). Strangely, the likelihood of the problem is affected by whether the Java graphics window is initially the "top" (Active) window (let's call that case "A" for Active) or is Below (case "B") another window such as Netscape. The KeyListener is almost always deaf in case A but almost never in case B.
Any helpful suggestions would be most appreciated. Included here is the complete (but stripped down) code.

 
Trailboss
Posts: 23773
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't tinkered much with key listener. But it looks like you set it up okay. Why not use some kind of button?
If I were experiencing that problem, I would make a program that does nothing but key listener stuff. If it doesn't work, then you know that maybe key listener is not supposed to be used that way. If it does work, then you know that key listener has problems with something else you are using.
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
I also agree with Bill. sometimes my keylistener also does not works. and if I take that code to another m/c It runs fine.
specifically I've seen that keyPressed()and keyReleased() are fired properly but keyTyped() misbehaves.& that too is not certain.

regards
deekasha
 
Bill Compton
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I haven't figured out why the original code was unreliable. However, I do have code that works, now. The change was to use a named class (rather than anonymous) that implements the KeyListener interface. Here's a snippet:

I suspect (but haven't tested) that a named class that extends the applicable Adapter would also have worked.
Possibly related: I found information about a known bug with the KeyListener mechanism on Win32 (where I am working at the moment). It's at: Bug 4186905
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have experienced the same problem, I got a JPanel which is used as a workspace for a diagram editor tool, and want that panel to listen to key events. Neither implementing KeyListener nor addKeyListener(new KeyAdapter) seemed to work..
I also tried what is described in the api; setFocusable(true) and requestFocus() which set focus to the component (in this case the panel), cause a component has to gain focus to be able to listen for key events..To write a small named class which does nothing but key listener stuff doesnt seem to work either..How is it, do we have to add a keylistener to a container, or isn't that necessary??
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent the setFocusable method seemed to work fine for me. I get key events from the applet init and not after the left mouse is clicked inside my panel.

Usinig AWT, Applet ....
 
Keith Lynch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it worked with 1.5 but the customer is limited to 1.1.8 so setFocusable went out the window. After a little thinking I added the requestFocus() method inside of the MouseEntered event. This way your panel always has focus as soon as the mouse enters the window and not as soon as the user clicks inside of the window. In my case I was adding [shift+click and drag] functionality to a group of cells in a form.
reply
    Bookmark Topic Watch Topic
  • New Topic