Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Trouble implementing multiple event handlers  RSS feed

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm having difficulty implementing keyboard and mouse event handlers at the same time for a program designed to notify the user when a key has been pressed or a mouse button clicked.

I can make and understand a program that does one or the other, but when I try to combine them, either the keyboard only or the mouse only works.

here is the code:

 
Bartender
Posts: 9494
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sean,

It doesn't have to do with both listeners being used at the same time. Without the KeyListener, the MouseListeners don't work for me either.

I think the problem is that events are only fired from components that have the focus. I'm not sure if your frame has focus when you're messing with components inside it, or whether it can be focused at all, but a solution would be to put the listeners on a component inside the frame, such as your JTextArea.
 
Stephan van Hulst
Bartender
Posts: 9494
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Swing.
 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sean,

first of all: your JFrame class has no main method, so I don't know how you get your
frame on the screen. There's no 'pack' and no 'setVisible(true)' in the constructor.
The mousepanel and the JLabel are nod added, too.

You only add the JTextArea to your frame, effectively adding it to its content pane.
It may be the case that this textarea fully covers your content pane, and since you
did not add the listener to the textarea, your clicks and mouse moves are not handled.

Apart from this, I think that what you did should work, provided there is some part
of the content pane on which you can click.

Also, when the handler is called in case of a mouse move or mouse click, you only
set line 1, so whatever was in line2 and line3 will still be displayed.

Greetz,
Piet

 
S Connor
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you; adding the mousepanel has enabled me to use both inputs at the same time
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, then happy clicking and typing! ;)

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!