2012-02-01 28 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

相關問題