tooltip consumes mouse events?

P

Peter Ashford

Hi there,

I have a jframe with a mouse listener on it. I use mouse events to
allow the user to drag the frame around the screen. This all works
fine, but I added some code to set a tooltip for the frame on the
mouse entered method like so:

public void mouseEntered(MouseEvent e){
Date now = new Date();
String toolTipTxt = String.format("%tA %te %tB %tY", now,
now, now, now);
frame.getRootPane().setToolTipText(toolTipTxt);
frame.setCursor(moveCursor);
}

....and this seems to end up consuming all of the mouse events, so that
I now cannot move my frame (nor launch the popup menu attached to it
via a rightclick action)

Is this correct behaviour? If so, how should one add a tooltip to a
frame with a mouselistener on it?

Thanks!

I'm using Java 1.6.
 
T

Thomas Hawtin

Peter said:
...and this seems to end up consuming all of the mouse events, so that
I now cannot move my frame (nor launch the popup menu attached to it
via a rightclick action)

(Popup events are not necessarily right click actions - you should test
all mouse events for isPopupTrigger.)
Is this correct behaviour? If so, how should one add a tooltip to a
frame with a mouselistener on it?

Unfortunately it is the expected broken behaviour for mouse and mouse
motion events. Adding a mouse listener stops the events bubbling up the
component-container hierarchy. Actually setComponentPopupMenu (since
1.5) should be alright as it uses AWTEventListeners.

Some of your choices are:

o Add the mouse listeners at exactly the same level as other mouse
listeners. You may need to play about with ContainerListener to achieve
this.
o Add an AWTEventListener. This requires security permissions.
o Push an EventQueue with an overridden dispatchEvent method. this
doesn't stack well with similar code and prevents system copy & paste
working in sandboxed environments.

Tom Hawtin
 
P

Peter Ashford

(Popup events are not necessarily right click actions - you should test
all mouse events for isPopupTrigger.)

Noted :eek:)
Unfortunately it is the expected broken behaviour for mouse and mouse
motion events. Adding a mouse listener stops the events bubbling up the
component-container hierarchy. Actually setComponentPopupMenu (since
1.5) should be alright as it uses AWTEventListeners.

I've switched to using a JWindow (didn't want a taskbar icon) and all
my mouse events are working, I'm just not getting the tooltip.
Some of your choices are:

o Add the mouse listeners at exactly the same level as other mouse
listeners. You may need to play about with ContainerListener to achieve
this.

I'm not quite sure what you mean by this - I'm not familiar with
ContainerListener - what should I do with it?

I figured that one other option is that since I'm getting the mouse
events ok, I could start a thread in MouseEntered waiting for a
hovering mouse and then throw up a custom tooltip (just another
undecorated JWindow)

Thanks for the thoughts, by the way - I appreciate the ideas.
 
T

Thomas Hawtin

Peter said:
I've switched to using a JWindow (didn't want a taskbar icon) and all
my mouse events are working, I'm just not getting the tooltip.

Not entirely sure why. Obviously the PL&F will add some mouse listeners
to components of a JFrame. Perhaps there is a problem with Window having
an owner despite it being a top-level component (which is a bit confusing).

Looking at the bug database it looks as if in 1.5 (only) "setToolTipText
does not work if the component is not focussed". The bug database is
down at the moment, but it is in the google cache.

http://64.233.183.104/search?q=cach...ava&hl=en&ct=clnk&cd=1&gl=uk&client=firefox-a
I'm not quite sure what you mean by this - I'm not familiar with
ContainerListener - what should I do with it?

If one adds a mouse listeners to a hierarchy of components, and then add
(or remove) a component, that component will need a listener added (or
removed). Container listeners will indicate when a component is added or
removed.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top