设置SWT工具提示延迟时间

3

在SWT中是否可以更改工具提示延迟时间?在Swing中,我通常会使用Tooltip.sharedInstance()中的方法。但是在SWT中似乎无法使用。

3个回答

7
我使用类似以下的代码。感谢 @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);
                    }
                });
            }
        });
    }
}

使用示例:SwtUtils.tooltip(button, "文本", "消息");


3
您可以使用以下内容:
ToolTip tip = new ToolTip(shell, SWT.BALLOON | SWT.ICON_INFORMATION);
tip.setText("Title");
tip.setMessage("Message");
tip.setAutoHide(false);

然后,每当您想要显示它时,请使用tip.setVisible(true)并启动一个计时器,该计时器将在指定时间后调用tip.setVisible(false)

tip.setAutoHide(false)强制提示保持显示状态,直到您调用tip.setVisible(false)为止。


3
不,据我所知不行。工具提示与底层本地系统的工具提示紧密耦合,因此您只能使用它们的行为。
但是还有另一种方法,您可以自己实现工具提示。通过这种方法,您可以创建非常复杂的工具提示。
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 );
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接