调整SWT TableColumn的大小以使其所有数据都可见

时间:2014-10-13 11:08:55

标签: java swt jface

我有一个Dialog和一个TableViewer。下面是我用来构造TableViewer的代码:

Composite tableComposite = new Composite(parent, SWT.NONE);
tableComposite.setLayout(new GridLayout(1, false));
tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));

TableViewer tableViewer = factory.buildTableViewer(tableComposite);
Table table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);

column1= new TableViewerColumn(tableViewer, SWT.NONE);
column1.getColumn().setText("Column1");
column1.getColumn().setResizable(false);

column2= new TableViewerColumn(tableViewer, SWT.NONE);
column2.getColumn().setText("Column2");
column2.getColumn().setResizable(false);

column3= new TableViewerColumn(tableViewer, SWT.NONE);
column3.getColumn().setText("Column3");
column3.getColumn().setResizable(false);

tableLayout = new TableColumnLayout();
tableComposite.setLayout(tableLayout);

layoutColumns();

下面是layoutColumns()设置表格布局的代码:

// Resize the columns to fit the contents
column1.getColumn().pack();
column2.getColumn().pack();
column3.getColumn().pack();

int column1Width = column1.getColumn().getWidth();
int column2Width = column2.getColumn().getWidth();
int column3Width = column3.getColumn().getWidth();

tableLayout.setColumnData(column1.getColumn(), new ColumnWeightData(0, rootObjFolderColumnWidth));
tableLayout.setColumnData(column2.getColumn(), new ColumnWeightData(0, column2));
tableLayout.setColumnData(column3.getColumn(), new ColumnWeightData(100, column3));

当我在column3中有大量数据时,我无法看到完整的文本,而是在最后得到...但是我希望用水平滚动条看到完整的文本。

我如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

请尝试:

table.setLayoutData(new GridData(GridData.FILL_BOTH)); 
//and then on each of the TableViewerColumn 
column1.getColumn().pack();

这是来自eclipsezone论坛的this link