• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble implementing multiple event handlers

 
S Connor
Greenhorn
Posts: 19
  • 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:

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • 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: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to Swing.
 
Piet Souris
Rancher
Posts: 1641
36
  • 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
Greenhorn
Posts: 19
  • 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
Rancher
Posts: 1641
36
  • 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!