pg-robban
pg-robban

Reputation: 1435

Setting SWT tooltip delays

Is it possible to change the tooltip delay in SWT? In Swing, I would normally use the methods in Tooltip.sharedInstance(). This seems to break in SWT.

Upvotes: 3

Views: 5621

Answers (3)

marioosh
marioosh

Reputation: 28556

I use something like below. Thanks to @Baz :)

public class SwtUtils {

    final static int TOOLTIP_HIDE_DELAY = 300;   // 0.3s
    final static int TOOLTIP_SHOW_DELAY = 1000;  // 1.0s

    public static void tooltip(final Control c, String tooltipText, String tooltipMessage) {

        final ToolTip tip = new ToolTip(c.getShell(), SWT.BALLOON);
        tip.setText(tooltipText);
        tip.setMessage(tooltipMessage);
        tip.setAutoHide(false);

        c.addListener(SWT.MouseHover, new Listener() {
            public void handleEvent(Event event) {
                tip.getDisplay().timerExec(TOOLTIP_SHOW_DELAY, new Runnable() {
                    public void run() {
                        tip.setVisible(true);
                    }
                });             
            }
        });

        c.addListener(SWT.MouseExit, new Listener() {
            public void handleEvent(Event event) {
                tip.getDisplay().timerExec(TOOLTIP_HIDE_DELAY, new Runnable() {
                    public void run() {
                        tip.setVisible(false);
                    }
                });
            }
        });
    }
}

Usage example: SwtUtils.tooltip(button, "Text", "Message");

Upvotes: 7

Baz
Baz

Reputation: 36884

You could use the following:

ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
tip.setText("Title");
tip.setMessage("Message");
tip.setAutoHide(false);

Then, whenever you want to show it, use tip.setVisible(true) and start a timer, which will call tip.setVisible(false) after a specified time.

tip.setAutoHide(false) forces the tip to stay until you call tip.setVisible(false).

Upvotes: 3

derBiggi
derBiggi

Reputation: 341

No, not as far as I know. The tooltips are tightly coupled to the tooltips of the underlying native system, so you're stuck with their behaviour.

But there is another way, you would have to implement the tooltips yourself. With that approach you can create very complex tooltips.

class TooltipHandler {
    Shell tipShell;

    public TooltipHandler( Shell parent ) {
        tipShell = new Shell( parent, SWT.TOOL | SWT.ON_TOP );

        <your components>

        tipShell.pack();
        tipShell.setVisible( false );
    }

    public void showTooltip( int x, int y ) {
        tipShell.setLocation( x, y );
        tipShell.setVisible( true );
    }

    public void hideTooltip() {
        tipShell.setVisible( false );
    }
}

Upvotes: 3

Related Questions