java - SWT 中工具提示的可见性

标签 java eclipse-plugin swt eclipse-rcp

我有一个显示标签的类。当我将鼠标悬停在该标签上时,会显示一个空的工具提示。我想删除该工具提示(我的意思是我不想显示它)。我可以轻松地说 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/

相关文章:

java - 我不明白为什么我的可扩展 ListView 的 onChildClick 方法出现致命异常

java - 使用java进行深度优先搜索

android - 是否有 eclipse 插件来生成 findViewById 调用?

java - 如何从 ICompilationUnit 获取 IEditorPart

java - 翻译 JFace 中的控件

java - 在 JLabel 扩展类中找不到符号

java - Hibernate session 关闭异常,当10个或更多用户访问页面时

eclipse - eclipse插件开发中如何隐藏文件?

Java/SWT Interface Builder/如何将图像设置为标签

java - SWT Shell 根据 child 调整大小