aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes KeyListener not working for JLabel. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener not working for JLabel." Watch "KeyListener not working for JLabel." New topic
Author

KeyListener not working for JLabel.

Sharad Kharya
Ranch Hand

Joined: Oct 15, 2008
Posts: 68
Hi,
I am working on Swing application and not able to add keyListener on my JLabel component.
But mouseListener is working properly.

I need to add 3 keyboard events
1) Enter - To get inside label folder
2) Ctrl – need to perform ctrl+click operation.
3) Tab – to navigate from one Label to another.(and after browsing to the selected label user can press enter key)

Below is the code snippet

Please let me know who to resolve keyboard event handling issue properly here.

Thanks
Sharad>
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

have you try comment out the mouse listener part and see if key stuff works? If it doesn't probably something wrong inside those overridden methods. If not try put key listener in front of mouse listener. Oh shouldn't the label be focused before key actions are detected? Just a thought.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Below is the code snippet

just interested to know how menu, toolbar, border, combobox, button, awt.frame, diaolg, popup and scrollpane,
has anything to do with the subject line?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

Sharad Golu wrote:Hi,
I am working on Swing application and not able to add keyListener on my JLabel component.
But mouseListener is working properly.

I need to add 3 keyboard events
1) Enter - To get inside label folder
2) Ctrl – need to perform ctrl+click operation.
3) Tab – to navigate from one Label to another.(and after browsing to the selected label user can press enter key)

I don't understand where the KeyListener comes into your design. You certainly don't need to listen for Tab keys, that's the job of the FocusListener for whatever component contains your JLabel. Likewise if the JLabel has focus, then pressing Enter will automatically do the same as if you clicked on the JLabel.

And I believe you would look for Ctrl-click in a MouseListener rather than a KeyListener.

I didn't look at any of your code because I don't think any of it was relevant (because of what I just posted above). But even if some of it was relevant, there was way too much of it. A small program with one or two labels would have sufficed. Anything else would just distract from the actual question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener not working for JLabel.