Dav*_*win 5 java swing tooltip
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;
}
}
Run Code Online (Sandbox Code Playgroud)
看来 hideTipAction 的 isEnabled() 属性直接与tipShowing 布尔值相关联。你可以试试这个:
public boolean isTooltipShowing(JComponent component) {
AbstractAction hideTipAction = (AbstractAction) component.getActionMap().get("hideTip");
return hideTipAction.isEnabled();
}
Run Code Online (Sandbox Code Playgroud)
您可能想要对空值等进行一些健全性检查。但这应该让您非常接近。
编辑您的回复:
如果没有一些丑陋的反射代码,我认为您没有太多选择。ToolTipManager由于包私有构造函数,你不能子类化,并且showTipWindow()和hideTipWindow()也是包私有的,所以适配器模式也被淘汰了。
| 归档时间: |
|
| 查看次数: |
914 次 |
| 最近记录: |