• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble implementing multiple event handlers

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to Swing.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you; adding the mousepanel has enabled me to use both inputs at the same time
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, then happy clicking and typing! ;)

Greetz,
Piet
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic