java-Swing可以告诉我是否有活动的工具提示吗?

前端之家收集整理的这篇文章主要介绍了java-Swing可以告诉我是否有活动的工具提示吗? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Swing中是否有一种优雅的方法来查找框架中当前是否显示任何工具提示

我使用的是自定义工具提示,因此在我的createToolTip()方法中设置标记非常容易,但是我看不到找到工具提示何时消失的方法.

ToolTipManager对此有一个很好的标记,tipShowing,但它当然是私有的,他们似乎没有提供实现它的方法. hideWindow()不会调出工具提示组件(可以告诉我),所以我看不到那里的方法.

有人有什么好主意吗?

更新:我进行了反思.您可以在此处查看代码

  1. private boolean isToolTipVisible() {
  2. // Going to do some nasty reflection to get at this private field. Don't try this at home!
  3. ToolTipManager ttManager = ToolTipManager.sharedInstance();
  4. try {
  5. Field f = ttManager.getClass().getDeclaredField("tipShowing");
  6. f.setAccessible(true);
  7. boolean tipShowing = f.getBoolean(ttManager);
  8. return tipShowing;
  9. } catch (Exception e) {
  10. // We'll keep silent about this for now,but obvIoUsly we don't want to hit this
  11. // e.printStackTrace();
  12. return false;
  13. }
  14. }
最佳答案
似乎hideTipAction的isEnabled()属性直接绑定到tipShowing布尔值.您可以尝试以下方法

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

您可能想要对null等进行完整性检查.但这应该可以使您更加接近.

编辑,对您的答复:

缺少一些难看的反射代码,我认为您没有太多选择.由于包私有的构造函数,您不能继承ToolTipManager的子类,并且showTipWindow()和hideTipWindow()也是包私有的,因此Adapter模式也不可用.

猜你在找的Java相关文章