David Irwin David Irwin - 1 year ago 63
Java Question

Can Swing tell me if there is an active tooltip?

Is there an elegantish way in Swing to find out if there are any tooltips currently being displayed in my frame?

I'm using custom tooltips, so it would be very easy to set a flag in my

method, but I can't see a way to find out when the tooltip is gone.

has a nice flag for this, tipShowing, but of course it's
and they don't seem to offer a way to get to it.
doesn't call out to the tooltip component (that I can tell), so I don't see a way there.

Anyone have any good ideas?

Update: I went with reflection. You can see the code here:

private boolean isToolTipVisible() {
// Going to do some nasty reflection to get at this private field. Don't try this at home!
ToolTipManager ttManager = ToolTipManager.sharedInstance();
try {
Field f = ttManager.getClass().getDeclaredField("tipShowing");

boolean tipShowing = f.getBoolean(ttManager);

return tipShowing;

} catch (Exception e) {
// We'll keep silent about this for now, but obviously we don't want to hit this
// e.printStackTrace();
return false;

Answer Source

It appears that the isEnabled() property of the hideTipAction is directly tied to the tipShowing boolean. You could try this:

public boolean isTooltipShowing(JComponent component) {
    AbstractAction hideTipAction = (AbstractAction) component.getActionMap().get("hideTip");
    return hideTipAction.isEnabled();

You probably want to do some sanity checking for nulls, etc. But this should get you pretty close.

EDIT, to your responses:

Short of some ugly reflection code, I don't think you have much choice. You cannot subclass ToolTipManager because of the package private constructor, and the showTipWindow() and hideTipWindow() are also package private, so the Adapter pattern is out as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download