Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing能否告诉我是否有激活的工具提示?_Java_Swing_Tooltip - Fatal编程技术网

Java Swing能否告诉我是否有激活的工具提示?

Java Swing能否告诉我是否有激活的工具提示?,java,swing,tooltip,Java,Swing,Tooltip,在Swing中是否有一种优雅的方式来确定当前是否有任何工具提示显示在我的框架中 我正在使用自定义工具提示,因此在我的createToolTip()方法中设置一个标志非常容易,但我看不到一种方法来确定工具提示何时消失 ToolTipManager在这方面有一个很好的标志,tipShowing,但是它当然是private,而且它们似乎没有提供一种方法来实现它hideWindow()不会调用工具提示组件(我可以告诉你),所以我看不出有什么办法 有人有什么好主意吗 更新:我带着反思去了。您可以在此处看到

在Swing中是否有一种优雅的方式来确定当前是否有任何工具提示显示在我的框架中

我正在使用自定义工具提示,因此在我的
createToolTip()
方法中设置一个标志非常容易,但我看不到一种方法来确定工具提示何时消失

ToolTipManager
在这方面有一个很好的标志,tipShowing,但是它当然是
private
,而且它们似乎没有提供一种方法来实现它
hideWindow()
不会调用工具提示组件(我可以告诉你),所以我看不出有什么办法

有人有什么好主意吗

更新:我带着反思去了。您可以在此处看到代码:

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;
    }
}

hideTipAction的isEnabled()属性似乎直接绑定到tipShowing布尔值。你可以试试这个:

public boolean isTooltipShowing(JComponent component) {
    AbstractAction hideTipAction = (AbstractAction) component.getActionMap().get("hideTip");
    return hideTipAction.isEnabled();
 }
您可能想对空值等进行一些健全性检查,但这应该会让您非常接近

编辑,以查看您的回复:


缺少一些丑陋的反射代码,我认为你没有太多选择。由于包私有构造函数的存在,您不能将
ToolipManager子类化,而
showTipWindow()
hideTipWindow()
也是包私有的,因此适配器模式也已失效。

看起来需要对所有组件进行循环,以查看它们是否有工具提示。我在寻找全球价值。也许循环是可行的,但它似乎效率低下。

那太糟糕了。经过内部讨论后,我们也想到了“丑陋的反射”,但我希望有人有更好的想法。

既然您已经有了自己的createToolTip(),也许您可以尝试这样的方法:)

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;
}