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 onl
FWIW, here is code that is based on the post by Noel. It takes that prior art and wraps it in a new class where the default is stored statically. Just in case anyone 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);
}
}