SWT 控件拒绝“占用多余的垂直空间”
情况是,TopTaskGroup(左一个)可以在调整窗口大小时“抓住多余的垂直空间”,但是NewTaskGroup(右侧),在其上添加 TooBar 后(请参阅 createAddBtnOnGroup 方法),它不会随着您调整窗口大小而增长。这是为什么?
(我有一个带有 2 列 GridLayout 的 shell 实例)
代码在这里:
private void createTaskWidgets() {
createTopTaskGroup();
createNewTaskGroup();
}
private void createTopTaskGroup() {
Group topTasksGroup = new Group(shell, SWT.SHADOW_NONE);
topTasksGroup.setText(TaskConsts.TOP_TASK_LIST);
topTasksTable = new TaskTable(topTasksGroup, TaskTable.SORT_BY_VOTES, iteration, this);
topTasksTable.setLayoutData(getTableGridData() );
topTasksGroup.setLayout(new GridLayout() );
topTasksGroup.setLayoutData(getTableGridData() );
topTasksGroup.pack();
}
private void createNewTaskGroup() {
Group newTasksGroup = new Group(shell, SWT.SHADOW_NONE);
newTasksGroup.setText(TaskConsts.NEW_TASK_LIST);
newTasksTable = new TaskTable(newTasksGroup, TaskTable.SORT_BY_CREATION_TIME, iteration, this);
topTasksTable.setLayoutData(getTableGridData() );
ToolBar actionToolBar = createAddBtnOnGroup(newTasksGroup);
newTasksGroup.setLayout(new GridLayout() );
newTasksGroup.setLayoutData(getTableGridData() );
newTasksGroup.layout();
newTasksGroup.pack();
// set actionToolBar's location to newTasksGroup's right-top position
actionToolBar.setLocation(
newTasksGroup.getLocation().x + newTasksGroup.getSize().x
- actionToolBar.getSize().x - 5,
newTasksGroup.getLocation().y - 2);
}
private GridData getTableGridData() {
GridData gridData = new GridData(0, SWT.FILL, false, true);
return gridData;
}
private ToolBar createAddBtnOnGroup(Group newTasksGroup) {
ToolBar actionToolBar = new ToolBar(newTasksGroup, SWT.HORIZONTAL | SWT.RIGHT);
addTaskToolItem = new ToolItem(actionToolBar, SWT.PUSH | SWT.RIGHT);
addTaskToolItem.setImage(new Image(display, TaskConsts.ICON_PLUS));
final MainWindow mainWindow = this;
addTaskToolItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new CreateTask(getShell(), mainWindow);
}
});
GridData gridData = new GridData();
gridData.exclude = true;
actionToolBar.setLayoutData(gridData);
actionToolBar.pack();
return actionToolBar;
}
private void organize() {
GridLayout gridLayout = new GridLayout(2, false);
shell.setLayout(gridLayout);
shell.pack();
}
提前致谢~
The case is that the TopTaskGroup(left one) can "grab excess vertical space" while resizing window, but the NewTaskGroup(the right one), after adding a TooBar on it(see the
createAddBtnOnGroup
method), it doesn't grow as you resize the window. Why is that?
(I have a shell instance with 2-column GridLayout)
Code is here:
private void createTaskWidgets() {
createTopTaskGroup();
createNewTaskGroup();
}
private void createTopTaskGroup() {
Group topTasksGroup = new Group(shell, SWT.SHADOW_NONE);
topTasksGroup.setText(TaskConsts.TOP_TASK_LIST);
topTasksTable = new TaskTable(topTasksGroup, TaskTable.SORT_BY_VOTES, iteration, this);
topTasksTable.setLayoutData(getTableGridData() );
topTasksGroup.setLayout(new GridLayout() );
topTasksGroup.setLayoutData(getTableGridData() );
topTasksGroup.pack();
}
private void createNewTaskGroup() {
Group newTasksGroup = new Group(shell, SWT.SHADOW_NONE);
newTasksGroup.setText(TaskConsts.NEW_TASK_LIST);
newTasksTable = new TaskTable(newTasksGroup, TaskTable.SORT_BY_CREATION_TIME, iteration, this);
topTasksTable.setLayoutData(getTableGridData() );
ToolBar actionToolBar = createAddBtnOnGroup(newTasksGroup);
newTasksGroup.setLayout(new GridLayout() );
newTasksGroup.setLayoutData(getTableGridData() );
newTasksGroup.layout();
newTasksGroup.pack();
// set actionToolBar's location to newTasksGroup's right-top position
actionToolBar.setLocation(
newTasksGroup.getLocation().x + newTasksGroup.getSize().x
- actionToolBar.getSize().x - 5,
newTasksGroup.getLocation().y - 2);
}
private GridData getTableGridData() {
GridData gridData = new GridData(0, SWT.FILL, false, true);
return gridData;
}
private ToolBar createAddBtnOnGroup(Group newTasksGroup) {
ToolBar actionToolBar = new ToolBar(newTasksGroup, SWT.HORIZONTAL | SWT.RIGHT);
addTaskToolItem = new ToolItem(actionToolBar, SWT.PUSH | SWT.RIGHT);
addTaskToolItem.setImage(new Image(display, TaskConsts.ICON_PLUS));
final MainWindow mainWindow = this;
addTaskToolItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new CreateTask(getShell(), mainWindow);
}
});
GridData gridData = new GridData();
gridData.exclude = true;
actionToolBar.setLayoutData(gridData);
actionToolBar.pack();
return actionToolBar;
}
private void organize() {
GridLayout gridLayout = new GridLayout(2, false);
shell.setLayout(gridLayout);
shell.pack();
}
Thanks in advance~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您出色的问题描述!
在我看来,这是一个简单的复制粘贴错误。
createNewTaskGroup 方法中的第四行不应该
是
Thanks for your excellent problem description!
It seems to me that this is a simple copy-paste bug.
The fourth line in your createNewTaskGroup method should not be
but