Java Swing: how to be notified if the tooltip is visible?

I have many components that set tool tips with JComponent.setToolTipText(...) . However, these tool tips often change depending on many events. I could call setToolTipText (...) every time one of these events happens; but I would rather just have a tooltip listener that notifies me of the tooltip so that I can update the tooltip if necessary. I cannot find one way or another to give the listener a hint, am I missing one?

* Note. My solution should be compatible with Java 1.4.2.

+4
source share
3 answers

In fact, a decent solution was found: override JComponent.getToolTipText() .

One disturbing nuance of this is the code from JComponent.setToolTipText ():

  public void setToolTipText(String text) { String oldText = getToolTipText(); putClientProperty(TOOL_TIP_TEXT_KEY, text); ToolTipManager toolTipManager = ToolTipManager.sharedInstance(); if (text != null) { if (oldText == null) { toolTipManager.registerComponent(this); } } else { toolTipManager.unregisterComponent(this); } } 

So, if you override getToolTipText to return some kind of dynamic value, it is better to return null on the first call, or the tooltip of your tool will not be registered in ToolTipManager.

+3
source

I am not sure if there is an easy way to get notified of this event. However, it looks like you might need to change your design. The need to update a large number of components with constantly changing tool tips seems strange and problematic. Perhaps just add a generic MouseListener that defines the message for the current component at the current time. This eliminates the need to constantly change all components.

+2
source

Override JComponent createToolTip (), this will act as a listener, whenever a tooltip is created, this method is called.

0
source

All Articles