GUI上某个Composite上有一个TableViewer,使用FillLayout,建立TableViewer时设定好列宽。现在当TableViewer的宽度增加时,原来的列宽没有发生变化,而是多了一列,该列的列宽随着TableViewer宽度的增加而增加。
请问如何能够使TableViewer的宽度变化时,各列的宽度随之有比例的变化呢。
谢谢!

解决方案 »

  1.   

    先参照 javax.swing.JTable.doLayout()public void doLayout()
      Causes this table to lay out its rows and columns. Overridden so that columns can be resized to accomodate a change in the size of a containing parent. Resizes one or more of the columns in the table so that the total width of all of this JTable's columns is equal to the width of the table. Before the layout begins the method gets the resizingColumn of the tableHeader. When the method is called as a result of the resizing of an enclosing window, the resizingColumn is null. This means that resizing has taken place "outside" the JTable and the change - or "delta" - should be distributed to all of the columns regardless of this JTable's automatic resize mode. If the resizingColumn is not null, it is one of the columns in the table that has changed size rather than the table itself. In this case the auto-resize modes govern the way the extra (or deficit) space is distributed amongst the available columns. 
    The modes are: 
      AUTO_RESIZE_OFF: Don't automatically adjust the column's widths at all. Use a horizontal scrollbar to accomodate the columns when their sum exceeds the width of the Viewport. If the JTable is not enclosed in a JScrollPane this may leave parts of the table invisible. 
      AUTO_RESIZE_NEXT_COLUMN: Use just the column after the resizing column. This results in the "boundary" or divider between adjacent cells being independently adjustable. 
      AUTO_RESIZE_SUBSEQUENT_COLUMNS: Use all columns after the one being adjusted to absorb the changes. This is the default behavior. 
      AUTO_RESIZE_LAST_COLUMN: Automatically adjust the size of the last column only. If the bounds of the last column prevent the desired size from being allocated, set the width of the last column to the appropriate limit and make no further adjustments. 
      AUTO_RESIZE_ALL_COLUMNS: Spread the delta amongst all the columns in the JTable, including the one that is being adjusted. 楼主可以再查查API。
    另,若是要根据Table宽度来指定列宽,可以这样试试:TableColumn column = table.getColumnModel().getColumn(i);
    int width = nViewTotalWidth * 20%; // 根据Table宽度按比例分配
    column.setMaxWidth(width);
    column.setMinWidth(width);
    column.setPreferredWidth(width);