强制显示 Java 工具提示
给定一个 JTextField(或任何与此相关的 JComponent),如何在没有用户直接输入事件的情况下强制显示该组件的指定工具提示?换句话说,为什么没有JComponent.setTooltipVisible(boolean)
?
Given a JTextField
(or any JComponent
for that matter), how would one go about forcing that component's designated tooltip to appear, without any direct input event from the user? In other words, why is there no JComponent.setTooltipVisible(boolean)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现的唯一方法(除了创建自己的工具提示窗口)是模拟焦点上的 CTRL+F1 击键:
不幸的是,一旦您移动鼠标(在组件之外)或延迟后,工具提示就会消失(请参阅
ToolTipManager.setDismissDelay
)。The only way (besides creating your own Tooltip window) I've found is to emmulate the CTRL+F1 keystroke on focus:
Unfortunately, the tooltip will disappear as soon as you move your mouse (outside of the component) or after a delay (see
ToolTipManager.setDismissDelay
).您需要调用默认操作来显示工具提示。例如,要在组件获得焦点时显示工具提示,您可以将以下 FocusListener 添加到组件:
编辑:
上面的代码似乎不再起作用。另一种方法是将 MouseEvent 分派给组件:
此方法将导致在显示工具提示之前出现轻微延迟,因为它模拟鼠标进入组件。要立即显示工具提示,您可以使用 pstanton 的解决方案。
You need to invoke the default Action to show the tooltip. For example to show a tooltip when a component gains focus you can add the following FocusListener to the component:
Edit:
The above code doesn't seem to work anymore. Another approach is dispatch a MouseEvent to the component:
This approach will result in a slight delay before the tooltip is displayed as it simulated the mouse entering the component. For immediate display of the tooltip you can use pstanton's solution.
对我来说,上面提到的类似版本有效(我使用 SwingUtilities 和 invokeLater 方法代替 Timer):
For me works the similar version stated above (instead of Timer I used SwingUtilities and invokeLater approach):
您可以访问 ToolTipManager,将初始延迟设置为 0 并向其发送事件。不要忘记事后恢复这些值。
You can access the
ToolTipManager
, set the initial delay to 0 and send it an event. Don't forget to restore the values afterwards.它不是
ToolTip
,但您可以使用气球提示:http://timmolderez.be/balloontip/doku.php
它正在通话中显示,有时候感觉比默认
ToolTip
更好It's not a
ToolTip
, but you can use a balloon tip:http://timmolderez.be/balloontip/doku.php
It is showing just on call and feels better in some moments then Default
ToolTip