java - 如何在不提供静态列宽的情况下创建具有多列的 Java SWT 表
问题描述
我目前正在开发一个 Eclipse 插件,在该插件中,有一个表单视图作为设计模板。在该表单视图中,我添加了一个表格,并且应该有两列的宽度比为 1:2。而且我还希望该表能够响应并动态更改其列宽以适应 formView 页面宽度。
以下代码段是我目前正在使用的代码段。
Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
fd = new FormData();
fd.height = 200;
fd.top = new FormAttachment(removeTestCaseButton, 5);
fd.left = new FormAttachment(1);
fd.right = new FormAttachment(99);
table.setLayoutData(fd);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column1 = new TableColumn(testCaseTable, SWT.CENTER);
column.setText("column One");
TableColumn column2 = new TableColumn(testCaseTable, SWT.CENTER);
column2.setText("column Two");
form.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle area = form.getBody().getClientArea();
int width = area.width;
column1.setWidth(width / 3);
column1.setWidth(width * 2 / 3);
}
});
但是这里的问题是当我打开 FormView 时它工作正常。但是我的桌子在Section里面。一旦我展开或折叠部分,表格宽度就会随着水平滚动条的出现而增加。
我只想要一个可靠的解决方案。
解决方案
TableViewer
使用带有TableColumnLayout
and的 JFace 更容易做到这一点ColumnWeightData
,但是您必须重新编写代码以使用 JFace 样式的内容和表格的标签提供程序。
TableColumnLayout tableLayout = new TableColumnLayout();
// A separate composite containing just the table viewer is required
Composite tableComp = new Composite(parent, SWT.NONE);
tableComp.setLayout(tableLayout);
TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
col1.getColumn().setText("Column 1");
col1.setLabelProvider(.... label provider for column 1 ....);
// Weight for column
tableLayout.setColumnData(col1.getColumn(), new ColumnWeightData(60));
TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
col2.getColumn().setText("Column 2");
col2.setLabelProvider(....... label provider for column 2 .....);
// Weight for column
tableLayout.setColumnData(col2.getColumn(), new ColumnWeightData(40));
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(.... input data for the viewer ....);
推荐阅读
- c++ - 在 C++ 中将浮点数转换为 int 的最快方法
- kubernetes - 如何访问 keycloak.local 入口主机?
- python - 如何更改当前视图的权限,覆盖 django rest-framework 中的 DEFAULT_PERMISSION_CLASSES
- templates - 在 smarty version3 中,我在文件夹结构中看到我们有 2 个插件目录,应该用于创建自定义插件功能?
- cluster-computing - rsh 无法访问集群节点
- android - 在应用程序中下载后如何安装apk,或弹出请求安装?
- deep-learning - 为什么 CNN 通常有一个词干?
- python - Python,滚动百分位数的快速计算
- angular - Asp .Net & Angular 11 - SignalR 404 错误
- sql - 试图从 SQL 数据库显示上个月的数据 - where 子句