在 Java Swing 中为特定组件设置工具提示延迟时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1190290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set the Tooltip Delay Time for a Particular Component in Java Swing
提问by Scottm
I'm trying to set tooltips on a JEditorPane
. The method which I use to determine what tooltip text to show is fairly CPU intensive - and so I would like to only show it after the mouse has stopped for a short amount of time - say 1 second.
我正在尝试在JEditorPane
. 我用来确定要显示的工具提示文本的方法是 CPU 密集型的 - 因此我只想在鼠标停止一小段时间后才显示它 - 比如说 1 秒。
I know I can use ToolTipManager.sharedInstance().setInitialDelay()
, however this will set the delay time for tooltips on all swing components at once and I don't want this.
我知道我可以使用ToolTipManager.sharedInstance().setInitialDelay()
,但是这会同时为所有摆动组件上的工具提示设置延迟时间,我不想要这个。
采纳答案by jjnguy
Well, I would recommend doing the CPU intensive task on another thread so it doesn't interrupt normal GUI tasks.
好吧,我建议在另一个线程上执行 CPU 密集型任务,这样它就不会中断正常的 GUI 任务。
That would be a better solution. (instead of trying to circumvent the problem)
那将是一个更好的解决方案。(而不是试图规避问题)
*Edit*You could possibly calculate the tootips for every word in the JEditorPane
and store them in a Map
. Then all you would have to do is access the tootip out of the Map
if it changes.
*编辑*您可能会计算 中每个单词的提示JEditorPane
并将它们存储在Map
. 然后,您所要做的就是在Map
它发生变化时访问totip 。
Ideally people won't be moving the mouse and typing at the same time. So, you can calculate the tootlips when the text changes, and just pull them from the Map
on mouseMoved()
.
理想情况下,人们不会同时移动鼠标和打字。因此,您可以在文本更改时计算 tootlips,然后将它们从Map
on 中拉出mouseMoved()
。
回答by Denis Tulskiy
You can show the popup yourself. Listen for mouseMoved() events, start/stop the timer and then show popup with the following code:
您可以自己显示弹出窗口。侦听 mouseMoved() 事件,启动/停止计时器,然后使用以下代码显示弹出窗口:
First you need PopupFactory, Popup, and ToolTip:
首先你需要 PopupFactory、Popup 和 ToolTip:
private PopupFactory popupFactory = PopupFactory.getSharedInstance();
private Popup popup;
private JToolTip toolTip = jEditorPane.createToolTip();
then, to show or hide the toolTip:
然后,显示或隐藏工具提示:
private void showToolTip(MouseEvent e) {
toolTip.setTipText(...);
int x = e.getXOnScreen();
int y = e.getYOnScreen();
popup = popupFactory.getPopup(jEditorPane, toolTip, x, y);
popup.show();
}
private void hideToolTip() {
if (popup != null)
popup.hide();
}
This will give you adjustable delay and a lot of troubles :)
这会给你可调延迟和很多麻烦:)
回答by Noel Grandin
If what you want is to make the tooltip dismiss delay much longer for a specific component, then this is a nice hack:
如果您想要的是使特定组件的工具提示关闭延迟更长,那么这是一个不错的技巧:
(kudos to tech at http://tech.chitgoks.com/2010/05/31/disable-tooltip-delay-in-java-swing/)
(感谢http://tech.chitgoks.com/2010/05/31/disable-tooltip-delay-in-java-swing/ 上的技术)
private final int defaultDismissTimeout = ToolTipManager.sharedInstance().getDismissDelay();
addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent me) {
ToolTipManager.sharedInstance().setDismissDelay(60000);
}
public void mouseExited(MouseEvent me) {
ToolTipManager.sharedInstance().setDismissDelay(defaultDismissTimeout);
}
});