2012-02-01 50 views
1

我知道这可以相当容易地实现,但我必须使用标准功能。 我需要一个工具提示才能显示在文本字段上,但只有当文本字段中的文本要显示在字段中时才会显示。调整列大小时,表格和树具有此功能,但我没有发现任何类似的文本字段。当控件中的文本太大而无法显示时,文本控件的SWT/JFace工具提示

我没有设法在Eclipse中找到这个功能,所以我猜这不是标准功能。 请证明我错了:)。

在此先感谢。

回答

3

这是什么意思“标准功能”..? (imo)标准添加modifyListenerText实例足够公平。

这里是我的方法

import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.ModifyEvent; 
import org.eclipse.swt.events.ModifyListener; 
import org.eclipse.swt.graphics.GC; 
import org.eclipse.swt.graphics.Point; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Text; 


public class TextLabel { 
    public TextLabel() { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout()); 
     shell.setSize(200, 150); 
     shell.setText("Long Text content label"); 

     Text txtLong = new Text(shell, SWT.SINGLE | SWT.BORDER); 
     txtLong.addModifyListener(new ModifyListener() { 

      @Override 
      public void modifyText(ModifyEvent e) { 
       Text txtSource = (Text) e.getSource(); 
       Point size = (new GC(txtSource)).stringExtent(txtSource.getText()); 
       if(size.x > txtSource.getBounds().width - txtSource.getBorderWidth()) txtSource.setToolTipText(txtSource.getText()); 
       else txtSource.setToolTipText(null); 
      } 
     }); 

     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
     display.dispose(); 
    } 

    public static void main(String args[]) { 
     new TextLabel(); 
    } 
} 
+0

你好,标准功能意味着开箱,酷似它是由表和树。我想,也许它是开箱即用的,我只需要激活它或什么。但是你的代码是corect并且正在工作。 – Lori 2012-02-03 09:42:32

+0

对。我并不清楚这个功能。 – Sorceror 2012-02-03 10:18:43

+0

基本上,如果你建立一个表或一棵树,调整一列的大小以使文本不再适合,你会看到这些组件提供了这个功能开箱即用。 – Lori 2012-02-03 10:24:54

相关问题