2011-04-27 43 views
0

我正在更改SWT TableViewer的字體大小,但表頭的高度不變,因此標籤被切斷。有沒有什麼辦法解決這一問題?SWT TableViewer標題高度

(這是Windows)

回答

4

Table談到在SWT和其他TableViewer談到在JFace。所以目前尚不清楚您是否需要TableViewerTable的解決方案。同樣在沒有任何代碼片段的情況下,我選擇演示JFaceTableViewer,但該概念對於TableSWT將保持不變。

>>設置字體

Before

>>設置字體

After

你可以看到頭部高度差之後之前。

>>代碼

import org.eclipse.jface.viewers.*; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.*; 
import org.eclipse.swt.graphics.*; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.widgets.*; 

public class TVHeaderTest 
{ 
    private class MyContentProvider implements IStructuredContentProvider { 
     public Object[] getElements(Object inputElement) { 
      return (MyModel[])inputElement; 
     } 
     public void dispose() { 
     } 
     public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 
     } 
    } 

    public class MyModel { 
     public int counter; 
     public MyModel(int counter) { 
      this.counter = counter; 
     } 
     public String toString() { 
      return "Item " + this.counter; 
     } 
    } 

    private static Display display; 

    public TVHeaderTest(final Shell shell) 
    { 
     final Table table = new Table (shell, SWT.H_SCROLL|SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION|SWT.CHECK); 
     table.setLinesVisible (true); 
     table.setHeaderVisible (true); 

     final String[] titles = {"!", "Description", "Resource", "In Folder", "Location"}; 
     for (int i=0; i<titles.length; i++) { 
      TableColumn column = new TableColumn (table, SWT.NONE); 
      column.setText (titles [i]); 
      column.setWidth(100); 
     } 

     final TableViewer v = new TableViewer(table); 
     v.setLabelProvider(new LabelProvider()); 
     v.setContentProvider(new MyContentProvider()); 


     MyModel[] model = createModel(); 
     v.setInput(model); 
     v.getTable().setLinesVisible(true); 


     Button button = new Button(shell, SWT.PUSH); 
     button.setText("Set Font"); 

     // FOCUS ON THIS PART - START 
     button.addSelectionListener(new SelectionListener() 
     { 
      public void widgetSelected(SelectionEvent e) 
      { 
       FontDialog d = new FontDialog(shell); 
       FontData data = d.open(); 
       table.setFont(new Font(display, data)); 
       for (int i = 0; i < titles.length; i++) { 
        table.getColumn(i).pack(); 
       } 

      } 
      public void widgetDefaultSelected(SelectionEvent e) { 
      } 
     }); 
     // FOCUS ON THIS PART - END 
     shell.pack(); 
    } 

    private MyModel[] createModel() { 
     MyModel[] elements = new MyModel[10]; 
     for(int i = 0; i < 10; i++) { 
      elements[i] = new MyModel(i); 
     } 
     return elements; 
    } 

    public static void main(String[] args) 
    { 
     display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new GridLayout()); 
     shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

     new TVHeaderTest(shell); 
     shell.open(); 

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

     display.dispose(); 

    } 

} 

結帳// FOCUS ON THIS PART - START// FOCUS ON THIS PART - END意見之間的代碼。

此外,你不能這樣做設置標題圖像。查看Table Header Image Bug

+0

感謝您的回覆!是的,我的意思是JFace中的TableViewer(我不知道JFace和SWT有什麼區別)。從我可以看到你的代碼和我一樣,使用.setFont()。但是在我的機器上(Windows XP),標題的高度保持不變。當我運行你的代碼時也會發生這種情況:http://www.abload.de/image.php?img=swttableviewerfzk5.png - 似乎是一個Windows問題。任何想法如何解決或解決這個問題? – Dexter 2011-04-27 23:28:39

+1

@Dexter:是的,它似乎是一個操作系統相關的問題。根據我發佈的** Bug **鏈接:'這是一個平臺限制。 Windows不會增長標題的高度。已知SWT可直接訪問操作系統小部件。所以窗口限制擴展到SWT。雖然,評論發佈於2006年,但我最好的猜測是它是用於XP的。作爲一個工作,請看http://sourceforge.net/projects/ktable/和http://www.eclipse.org/nebula/widgets/grid/grid.php。 – Favonius 2011-04-28 05:30:12