2015-05-19 73 views
0

以下代碼爲我提供了一個類似於下面圖片鏈接的構造。例如,我有一個主組,內有3個組。每個組都有一個包含3列和最小寬度爲200的表格。如果調整組合大小,表格和組也會調整大小。但是如果尺寸小於200,每個表格下面會出現一個滾動條。實際上我的目的是在桌子下面看到一個滾動條。我也嘗試過一個ScrolledComposite-在maingroup和更小的組之間的「層」,但它沒有奏效。 有人可以給我一個例子,我如何創建一個滾動條而不是3?在不強制滾動條的情況下調整swt-table的大小

謝謝!

圖片:http://www.bilder-upload.eu/show.php?file=d94e50-1432047327.jpg

public class GroupTable 
{ 
    private static Composite composite; 
    private static ArrayList<Table> tablesList = new ArrayList<Table>(); 
    private static int    minWidth = 100; 

    public static void main(String[] args) 
    { 
     final Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 

     composite = new Composite(shell, SWT.BORDER); 
     GridLayout layout = new GridLayout(1, false); 
     layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0; 
     composite.setLayout(layout); 

     // MainGroup 
     Group gr = new Group(composite, SWT.NONE); 
     GridLayout gLayout = new GridLayout(3, false); 
     gr.setLayout(gLayout); 
     GridData griddata = new GridData(SWT.FILL, SWT.FILL, true, false); 
     gr.setLayoutData(griddata); 
     gr.setText("Main Group"); 

     for (int i = 0; i < 3; i++) 
     { 
      createGroups(gr, layout, i); 
     } 

     composite.addControlListener(new ControlAdapter() 
     { 
      @Override 
      public void controlResized(ControlEvent e) 
      { 
       for (Table table : tablesList) 
       { 
        Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT); 
        ScrollBar vBar = table.getVerticalBar(); 
        Composite group = table.getParent(); 
        Rectangle area = group.getClientArea(); 
        Rectangle t1 = area; 
        Rectangle t2 = table.computeTrim(0, 0, 0, 0); 
        Point t3 = vBar.getSize(); 

        int width = t1.width - t2.width - t3.x; 
        if (size.y > area.height + table.getHeaderHeight()) 
        { 
         // Subtract the scrollbar width from the total column 
         // width if a vertical scrollbar will be required 
         Point vBarSize = vBar.getSize(); 
         width -= vBarSize.x; 
        } 

        TableColumn[] columnse = table.getColumns(); 
        int colCount = columnse.length; 
        Point oldSize = table.getSize(); 
        if (oldSize.x > area.width) 
        { 
         // table is getting smaller so make the columns 
         // smaller first and then resize the table to 
         // match the client area width 
         for (TableColumn column : columnse) 
         { 
          if (width/colCount < minWidth) 
          { 
           width = minWidth * colCount; 
          } 
          column.setWidth(width/3); 
         } 
         table.setSize(area.width, area.height); 
        } 
        else 
        { 
         // table is getting bigger so make the table 
         // bigger first and then make the columns wider 
         // to match the client area width 
         table.setSize(area.width, area.height); 
         for (TableColumn column : columnse) 
         { 
          if (width/colCount < minWidth) 
          { 
           width = minWidth * colCount; 
          } 
          column.setWidth(width/3); 
         } 
        } 
       } 
      } 
     }); 

     shell.setSize(900, 400); 
     shell.open(); 
     while (!shell.isDisposed()) 
     { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 

    private static void createGroups(Composite parentGroup, GridLayout layout, int i) 
    { 
     Group group = new Group(parentGroup, SWT.NONE); 
     group.setLayout(layout); 
     GridData griddata1 = new GridData(SWT.FILL, SWT.FILL, true, true); 
     group.setLayoutData(griddata1); 
     group.setText("group " + i); 
     createTables(group, i); 
    } 

    private static void createTables(Group parent, int tn) 
    { 
     Table table = new Table(parent, SWT.FULL_SELECTION); 
     tablesList.add(table); 
     TableViewer localTableViewer = new TableViewer(table); 
     localTableViewer.getControl().setLayoutData(
       new GridData(SWT.FILL, SWT.FILL, true, true)); 

     for (int j = 0; j < 3; j++) 
     { 
      TableViewerColumn column = new TableViewerColumn(localTableViewer, 
        SWT.NONE); 
      column.getColumn().setText("column " + j); 
      column.getColumn().pack(); 
     } 

     for (int i = 0; i < 10; i++) 
     { 
      TableItem item = new TableItem(table, SWT.NONE); 
      item.setText(new String[]{"i ", "am", "table " + tn}); 
     } 
     table.setHeaderVisible(true); 
    } 
} 

回答

1

下面是使用一個例子的ScrolledComposite

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 

    ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL); 
    scrolledComposite.setLayout(new FillLayout()); 

    Composite content = new Composite(scrolledComposite, SWT.NONE); 
    content.setLayout(new GridLayout(3, true)); 

    for(int i = 0; i < 3; i++) 
    { 
     Table table = new Table(content, SWT.BORDER); 
     table.setHeaderVisible(true); 
     table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 

     for(int col = 0; col < 3; col++) 
     { 
      TableColumn column = new TableColumn(table, SWT.NONE); 
      column.setText("Col " + col); 
     } 

     for(int row = 0; row < 10; row++) 
     { 
      TableItem item = new TableItem(table, SWT.NONE); 

      for(int col = 0; col < table.getColumnCount(); col++) 
      { 
       item.setText(col, "Item " + row + " " + col); 
      } 
     } 

     for(int col = 0; col < table.getColumnCount(); col++) 
     { 
      table.getColumn(col).pack(); 
     } 


    } 

    scrolledComposite.setContent(content); 
    scrolledComposite.setExpandHorizontal(true); 
    scrolledComposite.setExpandVertical(true); 
    scrolledComposite.setMinSize(content.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

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

看起來像這樣:

無滾動條需要:

enter image description here

一個滾動條:

enter image description here

+0

優秀的,這個作品!不幸的是,如果表格變大,列不會重新調整大小,但這沒問題!也許我會找到一個解決方案 – schmusehase

+0

@schmusehase [This](http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src /org/eclipse/swt/snippets/Snippet77.java),[this](http://stackoverflow.com/questions/3186340/swt-table-auto-resize-all-columns)或[this](http:/ /stackoverflow.com/questions/9211106/swt-table-layout-resize-the-column-of-a-table-to-fill-all-the-available-spac)可能會有所幫助。 – Baz

0

所以,我試圖混合代碼巴茲和我。結果是一個滾動條可調整大小的列:) 非常感謝!

private static ArrayList<Table> tablesList = new ArrayList<Table>(); 
private static int groupCount = 3; 
private static int colsCount = 3; 
private static int itemsCount = 10; 
private static boolean firstCheck = true; 

public static void main(String[] args) { 
    final Display display = new Display(); 
    final Shell shell = new Shell(display); 
    shell.setLayout(new FillLayout()); 

    ScrolledComposite scrolledComposite = new ScrolledComposite(shell, 
      SWT.V_SCROLL | SWT.H_SCROLL); 
    scrolledComposite.setLayout(new FillLayout()); 
    GridLayout layout = new GridLayout(1, false); 
    Composite composite = new Composite(scrolledComposite, SWT.NONE); 
    composite.setLayout(layout); 

    // // MainGroup 
    Group gr = new Group(composite, SWT.NONE); 
    GridLayout gLayout = new GridLayout(groupCount, false); 
    gr.setLayout(gLayout); 
    GridData griddata = new GridData(SWT.FILL, SWT.FILL, true, false); 
    gr.setLayoutData(griddata); 
    gr.setText("Main Group"); 

    for (int i = 0; i < groupCount; i++) { 
     createGroups(gr, layout, i); 
    } 

    scrolledComposite.setContent(composite); 
    scrolledComposite.setExpandHorizontal(true); 
    scrolledComposite.setExpandVertical(true); 
    scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, 
      SWT.DEFAULT)); 

    shell.setSize(900, 400); 
    shell.open(); 

    shell.addControlListener(new ControlAdapter() { 
     @Override 
     public void controlResized(ControlEvent e) { 
      resizeColumns(); 
     } 
    }); 

    if (firstCheck) { 
     resizeColumns(); 
     firstCheck = false; 
    } 

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

private static void resizeColumns() { 
    for (Table table : tablesList) { 
     TableColumn[] columns = table.getColumns(); 
     int colCount = columns.length; 
     for (TableColumn column : columns) { 
      column.setWidth((table.getSize().x - 5)/colCount); 
     } 
    } 
} 

private static void createGroups(Composite parentGroup, GridLayout layout, 
     int i) { 
    Group group = new Group(parentGroup, SWT.NONE); 
    group.setLayout(layout); 
    GridData griddata1 = new GridData(SWT.FILL, SWT.FILL, true, false); 
    group.setLayoutData(griddata1); 
    group.setText("group " + i); 
    createTables(group, i); 
} 

private static void createTables(Group parent, int tn) { 
    Table table = new Table(parent, SWT.FULL_SELECTION); 
    tablesList.add(table); 
    TableViewer localTableViewer = new TableViewer(table); 
    localTableViewer.getControl().setLayoutData(
      new GridData(SWT.FILL, SWT.FILL, true, true)); 

    for (int j = 0; j < colsCount; j++) { 
     TableViewerColumn column = new TableViewerColumn(localTableViewer, 
       SWT.NONE); 
     column.getColumn().setText("column " + j); 
     column.getColumn().pack(); 
    } 

    for (int i = 0; i < itemsCount; i++) { 
     TableItem item = new TableItem(table, SWT.NONE); 
     item.setText(new String[] { "i ", "am", "table " + tn }); 
    } 
    table.setHeaderVisible(true); 
} 
相關問題