在SWT中是否可以更改工具提示延迟时间?在Swing中,我通常会使用Tooltip.sharedInstance()中的方法。但是在SWT中似乎无法使用。
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, "文本", "消息");
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)
为止。
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 );
}
}