aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes KeyListener problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener problem" Watch "KeyListener problem" New topic
Author

KeyListener problem

Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Hi,
I'm actually able to get this ode to work but I just don't understand why one way works and the other doesn't - would appreciate it if someone could explain it to me...
So I've got a class that represents a tab in a tabbed pane (MapTab), this tab contains an instance of the DrawingCanvas class for drawing shapes. First, in the MapTab class, I register the drawingCanvas object to listen to mouse events (the DrawingCanvas class implements the MouseListener & MouseMotionListener interfaces. This works fine:



Then, I was trying to register drawingCanvas as a listener for key events in the same way, but it doesnt work.... the parts I changed:

In MapTab class:


DrawingCanvas class:


What does work, however, is this:

MapTab class:


DrawingCanvas class:


I don't get why the first method, which is the same as in case of mouse events, doesnt work...

would appreciate some comments, thanks!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> ..but I just don't understand why one way works and the other doesn't - would appreciate it if someone could explain it to me...

I see plenty of this in the code
...

what do you expect us to do with the code?
just sit there and read it, is our time worthless?

if you want a precis, at least post something that compiles.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Well you don't need to be rude, I only just joined the forum and don't necessarily know "the rules of not wasting your time". I actually wanted to save ppl time reading code by deleting the unrelevant parts because I thought that an experienced programmer would be able to see the problem straight away from the code.

If it helps, I'm posting the full class code below, it was enough to just ask for it in a polite way. I thought the website said "A friendly place for Java greenhorns" :-/



Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Well you don't need to be rude,

not rude, just realistic.

you have a problem, lines of '...' will rarely, if ever, get a solution.

you've now posted code that many eyes can copy/paste/compile/run/see your problem,
and, hopefully, present you with a solution.

Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Maybe realistic, but unfriendly.

I actually thought that lines of "..." would make the code more readable but I'm willing to accept that they do not do any good, advice taken. Thanks.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Actually you are correct we don't want to see all the code, but we don't want to see the "..." either.

You will find many postings that request a SSCCE, which it used to address both problems.

In general KeyEvents are not passed to a component unless it has focus and by default panels don't receive focus. One solution is to make the panel focusable, but this requires the user to click on the panel first. This is an old AWT approach.

The better solution is to use Key Bindings. This is the approach used by all Swing components. Read the section from the Swing tutorial on How to Use Key Bindings. You can also check out my little write up on Key Bindings.
Veronique Kus
Ranch Hand

Joined: Jun 13, 2010
Posts: 41
Hi Rob, thank you for the advice about the key bindings, I've managed to implement this method in my program. Still, I am just curious why trying to implement key event handling using the KeyListener did not work even though it worked in case of the MouseListener? I mean, I set the relevant JPanel to focusable and requested focus on every click on it but it still didn't work. If anyone has any thought what was wrong with it, please let me know.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener problem