【问题标题】:How can I make the columns fill all the table?如何使列填满所有表格?
【发布时间】:2016-10-05 12:02:38
【问题描述】:

我创建了一个 tableViewer,其中包含一个包含两列的表。事实是这两列并没有填满整个表格区域,我想创建两个具有相同比例的列来填充它。

代码:

TableViewer viewer = new TableViewer(createProjectConfig, SWT.MULTI | SWT.H_SCROLL| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
TableViewerColumn viewerColumn1 = new TableViewerColumn(viewer, SWT.NONE);

Table createVariablesTable = viewer.getTable();
createVariablesTable.setLayoutData(new FormData());
createVariablesTable.setHeaderVisible(true);
createVariablesTable.setLinesVisible(true);

FormData createVariablesTableData = new FormData();
createVariablesTableData.left = new FormAttachment(selectProjectPathLabel, 0, SWT.LEFT);
createVariablesTableData.top = new FormAttachment(selectProjectPathLabel, 40, SWT.TOP);
createVariablesTableData.right = new FormAttachment(selectProjectPathButton,0,SWT.RIGHT);
createVariablesTableData.bottom = new FormAttachment(createProjectConfigButton,-10,SWT.TOP);
createVariablesTable.setLayoutData(createVariablesTableData);

TableColumn column = viewerColumn.getColumn();
column.setText("${Variable}");
column.setWidth(50);//Make this fill the half of the table
column.setResizable(true);
column.setMoveable(true);

TableColumn column1 = viewerColumn1.getColumn();
column1.setText("${AAA}");
column1.setWidth(50);//Make this fill the half of the table
column1.setResizable(true);
column1.setMoveable(true);

图片:

【问题讨论】:

标签: java plugins swt jface


【解决方案1】:

您需要为表格设置表格布局。

TableLayout tableLayout = new TableLayout();
createVariablesTable.setLayout(tableLayout);

然后为每一列设置一个列权重:

tableLayout.addColumnData(new ColumnWeightData(50));

tableLayout.addColumnData(new ColumnWeightData(50));

同时删除对column.setWidth(50)的调用

【讨论】:

  • 嗨,setColumnData 给我带来了问题(它似乎没有被定义为 TableLayout 方法),我使用 addColumnData 使其工作:tableLayout.addColumnData(new ColumnWeightData(50));
  • 对不起,应该是addColumnDatasetColumnData 用于稍微复杂的TableColumnLayout。更新了答案。
  • 谢谢你,你帮了我很多!我已经看到 Baz 在我的评论下方发布的链接,但它使用 TableColumnLayout 在调整窗口大小时保持列的比例。有什么办法可以用 TableLayout 做同样的事情,或者我应该按照这个例子?
  • 如果您想更好地处理大小调整,请使用TableColumnLayout。主要区别在于布局必须设置在Composite 上,该表具有唯一的子表。
猜你喜欢
  • 2011-10-31
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多