gpt4 book ai didi

java - 使用单列格式化 SWT 表

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:31 25 4
gpt4 key购买 nike

我有下表:

enter image description here

我想要做的是将列扩展到窗口的大小,而不显示没有列的额外行(项目列右侧的单元格)。我对 SWT 的大部分内容有些熟悉,但这些表格仍然让我感到困惑。

我希望能够通过单击行中的任意位置来选择一个项目。

这是我用来制作上述屏幕截图的简单 UI 代码:

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

Table table = new Table (shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible (true);
table.setHeaderVisible (false);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200;
table.setLayoutData(data);

TableColumn column = new TableColumn (table, SWT.NONE);
column.setResizable(true);
int count = 15;
for (int i=0; i<count; i++) {
TableItem item = new TableItem (table, SWT.NONE);
item.setText (0, "item " + i);
}
column.pack();

shell.pack();
shell.open();

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

最佳答案

使您的表成为其父表的唯一子表,并创建一个包含以下内容的列:

import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.layout.TableColumnLayout;

public class ColumnHelper {
public static void createColumn(Table table) {
TableColumnLayout layout = new TableColumnLayout();
table.getParent().setLayout(layout);
TableColumn column = new TableColumn (table, SWT.NONE);
layout.setColumnData(column, new ColumnWeightData(10));
}
}

如果您遇到 strange, growing resize behaviour表格,将 TableColumnLayout 替换为:

/** Forces table never to take more space than parent has*/
public class TableColumnLayout extends org.eclipse.jface.layout.TableColumnLayout {

@Override
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
return new Point(wHint, hHint);
}
}

关于java - 使用单列格式化 SWT 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25652892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com