我有一个显示标签的类。当我将鼠标悬停在该标签上时,会显示一个空的工具提示。我想删除该工具提示(我的意思是我不想显示它)。我可以轻松地说 tooltip.setVisibility(false) 但我不应该更改 MouseTrackListener Anonymous 类中的代码。我需要在其他任何地方使用 toltip 属性,以便当我扩展此类时,我需要有一个选项可以轻松设置此工具提示的可见性(如果需要)或在不需要时禁用它。
这是我的代码片段(JAVA SWT)
tooltip = new ToolTip(parent.getShell(), SWT.NONE);
MouseTrackListener mouseTrackListener = new MouseTrackListener() {
@Override
public void mouseEnter(MouseEvent e) {
if (text != null && !text.isEmpty()) {
tooltip.setLocation(Display.getCurrent().getCursorLocation().x,
Display.getCurrent().getCursorLocation().y + TOOLTIP_OFFSET_Y);
tooltip.setVisible(true);
}
}
@Override
public void mouseExit(MouseEvent e) {
if (text != null && !text.isEmpty()) {
tooltip.setVisible(false);
}
}
@Override
public void mouseHover(MouseEvent e) {
}};
label.addMouseTrackListener(mouseTrackListener);
iconLabel.addMouseTrackListener(mouseTrackListener);
最佳答案
不太确定你想做什么。只是隐藏空工具提示或控制何时显示提示? 也许这适合您的需求:
private boolean showToolTip = true;
tooltip = new ToolTip(parent.getShell(), SWT.NONE);
public void setShowToolTip (boolean show){
showToolTip = show;
}
MouseTrackListener mouseTrackListener = new MouseTrackListener() {
@Override
public void mouseEnter(MouseEvent e) {
if (text != null && !text.isEmpty()) {
tooltip.setLocation(Display.getCurrent().getCursorLocation().x,
Display.getCurrent().getCursorLocation().y + TOOLTIP_OFFSET_Y);
if (showToolTip){
tooltip.setVisible(true);
} else {
tooltip.setVisible(false);
}
}
或者您想要一个不更改 MouseTrackListerner 实例中的代码的解决方案?
关于java - SWT 中工具提示的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22032693/