** 请注意,不鼓励使用HORIZONTAL_ALIGN_FILL 和GRAB_HORIZONTAL。相反,您应该使用 public GridData(int, int, boolean, boolean) 构造函数。 **
为了稍微简化您的代码 sn-p(组合中只有一列,并且只有一默认表列 - 请参阅下面的完整代码):
// ...
final Composite compositeArea = new Composite(parent, SWT.NONE);
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
compositeArea.setLayout(new GridLayout());
final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
// ...
...我们看到Table 不适合可用空间或未按预期显示滚动条,当Shell 调整大小时也会发生同样的情况。
在回答您的问题时,这是因为 Table 的布局数据不知道如何布局 vertically - 您只指定了两个 horizontal样式属性。
如果我们改为使用建议的构造函数:
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
...Table 正确占用可用空间,显示滚动条,并在调整 Shell 大小时正确更新。使用布局数据,我们告诉Table 填充可用的水平空间,并且Table 将在必要时显示滚动条。
完整示例:
public class TableResizeTest {
private final Display display;
private final Shell shell;
public TableResizeTest() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setMaximized(true);
final Composite parent = new Composite(shell, SWT.NONE);
parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
parent.setLayout(new GridLayout());
// -- snippet --
final Composite compositeArea = new Composite(parent, SWT.NONE);
compositeArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
compositeArea.setLayout(new GridLayout());
final Table table = new Table(compositeArea, SWT.BORDER | SWT.V_SCROLL);
// table.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL));
table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// -------------
for (int i = 0; i < 20; ++i) {
new TableItem(table, SWT.NONE).setText(String.valueOf(i));
}
}
public void run() {
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String... args) {
new TableResizeTest().run();
}
}