2012-02-09 72 views
4

我在Eclipse SWT中有5個列的表。我調整了窗口的大小,並與整個表格一起,但是列沒有被調整大小以填充所有可用空間。調整表格的列以填充所有可用空間

是否有一種佈局方法可以用來使列自動調整大小以填充所有可用空間?我發現了一些代碼,當客戶端空間被調整大小時,這些代碼會調整列的大小,但對我來說這似乎是一個小小的破解。

確實必須有一個體面的優雅的方式來做到這一點,通過使用佈局本身。

回答

5

這可能會派上用場:

Composite tableComposite = new Composite(parent, SWT.NONE); 
TableViewer xslTable = new TableViewer(tableComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); 
xslTable.getTable().setLinesVisible(true); 
xslTable.getTable().setHeaderVisible(true); 
TableViewerColumn stylesheetColumn = new TableViewerColumn(xslTable, SWT.NONE); 
stylesheetColumn.getColumn().setText(COLUMN_NAMES[0]); 
stylesheetColumn.getColumn().setResizable(false); 
TableViewerColumn conceptColumn = new TableViewerColumn(xslTable, SWT.NONE); 
conceptColumn.getColumn().setText(COLUMN_NAMES[1]); 
conceptColumn.getColumn().setResizable(false); 
TableColumnLayout tableLayout = new TableColumnLayout(); 
tableComposite.setLayout(tableLayout); 

layoutTableColumns(); 

layoutTableColumns方法

/** 
    * Resize table columns so the concept column is packed and the stylesheet column takes the rest of the space 
    */ 
    private void layoutTableColumns() 
    { 
    // Resize the columns to fit the contents 
    conceptColumn.getColumn().pack(); 
    stylesheetColumn.getColumn().pack(); 
    // Use the packed widths as the minimum widths 
    int stylesheetWidth = stylesheetColumn.getColumn().getWidth(); 
    int conceptWidth = conceptColumn.getColumn().getWidth(); 
    // Set stylesheet column to fill 100% and concept column to fit 0%, but with their packed widths as minimums 
    tableLayout.setColumnData(stylesheetColumn.getColumn(), new ColumnWeightData(100, stylesheetWidth)); 
    tableLayout.setColumnData(conceptColumn.getColumn(), new ColumnWeightData(0, conceptWidth)); 
    } 
+0

這裏還有一個片斷:http://www.volanakis.de/nuggets/Snippet77withTableColumnLayout.java – 2014-11-24 12:42:03

+2

這個答案會更好,如果它開始以某種總結。例如:「*這可以通過使用'TableLayout'和...... blah,blah *」來解決。 – Lii 2016-09-15 09:50:23

+0

非常感謝,作品像魅力! – Markus 2018-03-05 10:10:22

1

這是什麼,我試圖和它工作正常。

viewer.getControl().addControlListener(new ControlListener() { 

     @Override 
     public void controlResized(ControlEvent arg0) { 
      Rectangle rect = viewer.getTable().getClientArea(); 
      if(rect.width>0){ 
       int extraSpace=rect.width/4; 
       col1.getColumn().setWidth(extraSpace); 
       col2.getColumn().setWidth(extraSpace); 
       col3.getColumn().setWidth(extraSpace); 
       col4.getColumn().setWidth(extraSpace); 
      } 
     } 

     @Override 
     public void controlMoved(ControlEvent arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 
+0

感謝您的解決方案。節省了很多時間。 – 2018-02-27 10:44:16

相關問題