• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

KeyListener problem

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> ..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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic