在Java Swing中为特定组件设置工具提示延迟时间。

问题描述 投票:26回答:4

我正在尝试在一个网站上设置工具提示。JEditorPane. 我用来决定显示什么工具提示文字的方法是相当耗费CPU的--所以我想只在鼠标停止了很短的时间后才显示它--比如说1秒钟。

我知道我可以使用 ToolTipManager.sharedInstance().setInitialDelay()然而,这将一次性在所有的摆动组件上设置工具提示的延迟时间,我不希望这样。

java performance swing user-interface tooltip
4个回答
7
投票

好吧,我建议在另一个线程上做这个CPU密集型任务,这样就不会中断正常的GUI任务。

这将是一个更好的解决方案。(而不是试图规避这个问题)

*编辑* 你可以计算出每一个词的tootips,在 JEditorPane 并将其储存在 Map. 然后,你所要做的就是访问tootip出来的。Map 如果它发生变化。

理想情况下,人们不会同时移动鼠标和打字。 所以,你可以计算文本变化时的齿点,然后从下面的列表中提取它们。Map 关于 mouseMoved().


29
投票

如果你想要的是让工具提示对某一特定组件的解雇延迟时间更长,那么这就是一个不错的黑客。

(感谢技术在 http:/tech.chitgoks.com20100531disable-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);
  }
});

6
投票

你可以自己显示弹出窗口。侦听mouseMoved()事件,启动停止计时器,然后用下面的代码来显示弹出窗口。

首先,你需要PopupFactory,Popup和ToolTip。

private PopupFactory popupFactory = PopupFactory.getSharedInstance();
private Popup popup;
private JToolTip toolTip = jEditorPane.createToolTip();

然后,显示或隐藏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();
}

这将给你带来可调节的延迟和很多麻烦:)


0
投票

FWIW,这里是基于Noel的帖子的代码。它采用了先前的技术,并将其封装在一个新的类中,默认值是静态存储的。以防有人受益。

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

/**
 * Provides customizable tooltip timeouts for a {@link javax.swing.JComponent}.
 *
 * @see ToolTipManager#setDismissDelay(int).
 */
public final class CustomTooltipDelayer extends MouseAdapter
{
  private static final int defaultDismissTimeout = ToolTipManager.sharedInstance().getDismissDelay();

  private final int _delay;

  /**
   * Override the tooltip timeout for the given component in raw millis.
   *
   * @param component target
   * @param delay the timeout duration in milliseconds
   */
  public static CustomTooltipDelayer attach(JComponent component, int delay)
  {
    CustomTooltipDelayer delayer = new CustomTooltipDelayer(delay);
    component.addMouseListener( delayer );
    return delayer;
  }

  /**
   * Override the tooltip timeout for the given component as a ratio of the JVM-wide default.
   *
   * @param component target
   * @param ratio the timeout duration as a ratio of the default
   */
  public static CustomTooltipDelayer attach(JComponent component, float ratio)
  {
    return attach( component, (int)(defaultDismissTimeout * ratio) );
  }

  /** Use factory method {@link #attach(JComponent, int)} */
  private CustomTooltipDelayer(int delay)
  {
    _delay = delay;
  }

  @Override
  public void mouseEntered( MouseEvent e )
  {
    ToolTipManager.sharedInstance().setDismissDelay(_delay);
  }

  @Override
  public void mouseExited( MouseEvent e )
  {
    ToolTipManager.sharedInstance().setDismissDelay(defaultDismissTimeout);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.