2016-11-24 74 views
0

我有一個帶有表格和按鈕組件的嚮導。當我點擊添加按鈕時,我從對話框窗口中選擇應該添加的項目。我確認項目,然後這個用滾動條出現在表格中。但是如果我調整嚮導的大小,表格大小就會改變。如何解決它?大小調整之前調整SWT表格

表:

enter image description here

表嚮導後調整

Table afterresize

Composite compositeArea = new Composite(parent, SWT.NONE); 
GridLayout layout = new GridLayout(); 
layout.numColumns = 3; 
compositeArea.setLayout(layout); 
Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL); 
new TableColumn(someList, SWT.NULL); 
table.setLayoutData(new GridData(HORIZONTAL_ALIGN_FILL | GRAB_HORIZONTAL)); 
+0

這是否幫助:[鏈接](http://stackoverflow.com/questions/7740387/swt-java-how-to-prevent-window-from-調整)。注意關於事件處理程序的答案... – MordechayS

+1

所以你不想讓表格調整對話框的大小?你可以設置'GridData#widthHint'和'GridData#heightHint',但我不會建議,除非必要。 – Baz

回答

0

**請注意,使用HORIZONTAL_ALIGN_FILLGRAB_HORIZONTAL氣餒。相反,你應該使用public GridData(int, int, boolean, boolean)構造函數。 **


要性能稍微簡化您的代碼段(僅適用於複合一列,且只有一個默認的表列 - 參見下面的完整代碼):

// ... 
final Composite compositeArea = new Composite(parent, SWT.NONE); 
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
compositeArea.setLayout(new GridLayout()); 

final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL); 
table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 
// ... 

enter image description here

.. 。我們看到Table不適合可用空間或按預期顯示滾動條,並且在調整Shell的大小時也會發生相同情況。

在回答你的問題,這是因爲Table的佈局數據不知道如何佈局垂直 - 你只指定了兩個水平樣式屬性。

如果我們改用建議的構造函數:

table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

enter image description here

...的Table採取正確的可用空間,顯示滾動條,並更新正確時Shell調整大小。使用佈局數據,我們告訴Table填充可用的水平空間,如果需要,Table將顯示滾動條。


完整的示例:

public class TableResizeTest { 

    private final Display display; 
    private final Shell shell; 

    public TableResizeTest() { 
     display = new Display(); 
     shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     shell.setMaximized(true); 

     final Composite parent = new Composite(shell, SWT.NONE); 
     parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
     parent.setLayout(new GridLayout()); 

     // -- snippet -- 
     final Composite compositeArea = new Composite(parent, SWT.NONE); 
     compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
     compositeArea.setLayout(new GridLayout()); 

     final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL); 
     // table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL)); 
     table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
     // ------------- 

     for (int i = 0; i < 20; ++i) { 
      new TableItem(table, SWT.NONE).setText(String.valueOf(i)); 
     } 
    } 

    public void run() { 
     shell.setSize(300, 300); 
     shell.open(); 

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

    public static void main(final String... args) { 
     new TableResizeTest().run(); 
    } 

}