2015-02-09 93 views
0

我創建了一個CheckboxTableViewer來顯示如下圖所示的東西列表。問題是表中的滾動條不能正常工作。可能是我缺少表格的一些佈局參數?jface的佈局設置表

Table

這裏是代碼:

public class TableViewerClass { 


CheckboxTableViewer tableViewer; 

private GridData gridData; 

private List<String> checkListNames = new ArrayList<String>(); 

public TableViewerClass(Shell parent){ 

    checkListNames.add("Function Trace 1"); 
    checkListNames.add("Function Trace 2"); 
    checkListNames.add("Function Trace 3"); 
    checkListNames.add("Function Trace 4"); 
    checkListNames.add("Function Trace 5"); 
    checkListNames.add("Function Trace 6"); 

    createCheckViewer(parent); 
    createControlBox(parent); 
} 


private void createCheckViewer(Composite parent){ 

    tableViewer = CheckboxTableViewer.newCheckList(parent, SWT.SINGLE| SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL); 

    gridData = new GridData(SWT.TOP, SWT.TOP, false, false, 3, 1); 

    tableViewer.add(checkListNames.get(0)); 
    tableViewer.add(checkListNames.get(1)); 
    tableViewer.add(checkListNames.get(2)); 
    tableViewer.add(checkListNames.get(3)); 
    tableViewer.add(checkListNames.get(4)); 
    tableViewer.add(checkListNames.get(5)); 


    tableViewer.add(checkListNames.get(0)); 
    tableViewer.add(checkListNames.get(1)); 
    tableViewer.add(checkListNames.get(2)); 
    tableViewer.add(checkListNames.get(3)); 
    tableViewer.add(checkListNames.get(4)); 
    tableViewer.add(checkListNames.get(5)); 

    tableViewer.add(checkListNames.get(0)); 
    tableViewer.add(checkListNames.get(1)); 
    tableViewer.add(checkListNames.get(2)); 
    tableViewer.add(checkListNames.get(3)); 
    tableViewer.add(checkListNames.get(4)); 
    tableViewer.add(checkListNames.get(5)); 

    // define layout for the viewer 
    GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1); 
    tableViewer.getControl().setLayoutData(gridData); 


    final Table table = tableViewer.getTable(); 

    TableLayout layout = new TableLayout(); 


    TableViewerColumn col = new TableViewerColumn(tableViewer, SWT.LEAD); 
    col.getColumn().setText("Text"); 
    layout.addColumnData(new ColumnWeightData(500)); 

    table.setLayout(layout); 
    table.setHeaderVisible(true); 
    table.setLinesVisible(true); 
} 

private void createControlBox(Composite parent){ 

    Group group = new Group(parent, SWT.NONE); 
    group.setText("Control Box"); 
    gridData = new GridData(SWT.LEFT, SWT.TOP, true, false,1,1); 
    group.setLayoutData(gridData); 

    FillLayout fillLayout = new FillLayout(); 
    fillLayout.type = SWT.VERTICAL; 
    group.setLayout(fillLayout); 

    Button button = new Button(group, SWT.PUSH); 
    button.setText("Select All"); 

    Button button2 = new Button(group, SWT.PUSH); 
    button2.setText("Deselect All"); 

    button.addSelectionListener(this.getSelectAllHandler()); 
    button2.addSelectionListener(this.getDeselectAllHandler()); 
} 

private SelectionListener getSelectAllHandler(){ 

    SelectionListener selectAll = new SelectionListener(){ 
     @Override 
     public void widgetSelected(SelectionEvent e) { 

      tableViewer.setAllChecked(true); 
     } 

     @Override 
     public void widgetDefaultSelected(SelectionEvent e) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    return selectAll; 

} 

private SelectionListener getDeselectAllHandler(){ 

    SelectionListener deselectAll = new SelectionListener(){ 
     @Override 
     public void widgetSelected(SelectionEvent e) { 

      tableViewer.setAllChecked(false); 
     } 

     @Override 
     public void widgetDefaultSelected(SelectionEvent e) { 
      // TODO Auto-generated method stub 

     } 
    }; 

    return deselectAll; 

} 


public static void main(String[] args) { 


    Display display = new Display(); 
    final Shell shell = new Shell(display); 

    shell.setSize(500, 400); 

    GridLayout layout = new GridLayout(4, false); 

    shell.setLayout(layout); 

    shell.setText("JFace Table Example"); 
    new TableViewerClass(shell); 

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

} 

回答

1

shell.pack()調用重新計算殼的大小,使其適合內容。

要限制你需要指定表上的高度提示表的大小:

GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false, 3, 1); 

gridData.heightHint = 400; // Vertical size you want 

tableViewer.getControl().setLayoutData(gridData);