Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

MyKeyListener won't listen to me  RSS feed

 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a desktop application with a JFrame holding a JPanel depicting all sorts of lines and circles.

I can click on the JPanel and call forth all kinds of functionality with my own MyMouseListener,
however instead of just leftClick and rightClick,
I'd like to do Ctrl + LeftClick or 'A' + RightClick to expand functionality.

MyMouseListener works just fine. However MyKeyListener takes no notice of when I use the keyboard.
JPanel is a JComponent, so why doesnt it react to keyboard usage?
Am I on the wrong track?



 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at this thread from last week, where somebody else had a similar problem. If you follow the tutorials link in my first post, it tells you about key listeners, and says something about the Component having to be focusable.
That may be your problem.
 
Dan Kempten
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, this setFocusable(...) was new to me.

I added this in the ctor and now it runs just fine.

 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
It appeared to be what happened last week, again.

BTW: Abbreviations like ctor can be difficult to understand.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!