[ACCEPTED]-Set the Tooltip Delay Time for a Particular Component in Java Swing-tooltip

Accepted answer
Score: 33

If what you want is to make the tooltip 3 dismiss delay much longer for a specific 2 component, then this is a nice hack:

(kudos 1 to tech at 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);
  }
});
Score: 7

Well, I would recommend doing the CPU intensive 12 task on another thread so it doesn't interrupt 11 normal GUI tasks.

That would be a better 10 solution. (instead of trying to circumvent 9 the problem)

*Edit* You could possibly calculate 8 the tootips for every word in the JEditorPane and store 7 them in a Map. Then all you would have to 6 do is access the tootip out of the Map if it 5 changes.

Ideally people won't be moving the 4 mouse and typing at the same time. So, you 3 can calculate the tootlips when the text 2 changes, and just pull them from the Map on 1 mouseMoved().

Score: 7

You can show the popup yourself. Listen 6 for mouseMoved() events, start/stop the 5 timer and then show popup with the following 4 code:

First you need PopupFactory, Popup, and 3 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 2 will give you adjustable delay and a lot 1 of troubles :)

Score: 0

FWIW, here is code that is based on the 4 post by Noel. It takes that prior art and 3 wraps it in a new class where the default 2 is stored statically. Just in case anyone 1 may benefit:

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);
  }
}

More Related questions