Can Swing tell me if there is an active tooltip?

Is there an elegant way in Swing to find out if there are tooltips displayed in my frame?

I use my own tooltips, so it would be very easy to set a flag in my method createToolTip(), but I don’t see a way to find out when the tooltip disappeared.

ToolTipManagerhas a good flag for this, tipShowing, but of course it is private, and they don't seem to offer a way to get to it. hideWindow()does not call the tooltip component (what can I say), so I do not see the 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");
        f.setAccessible(true);

        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;
    }
}
+3
4

, isEnabled() hideTipAction tipShowing boolean. :

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

, , .. .

, :

, , . ToolTipManager - , showTipWindow() hideTipWindow() , .

+3

, , , . . , , .

0

. " " , , , - .

0

Since you already have your own createToolTip (), maybe you can try something like this :)

public JToolTip createToolTip() {
  JToolTip tip = super.createToolTip();
  tip.addAncestorListener( new AncestorListener() {
    public void ancestorAdded( AncestorEvent event ) {
      System.out.println( "I'm Visible!..." );
    }

    public void ancestorRemoved( AncestorEvent event ) {
      System.out.println( "...now I'm not." );
    }

    public void ancestorMoved( AncestorEvent event ) { 
      // ignore
    }
  } );
  return tip;
}
0
source

All Articles