Reputation: 39
the program attached below creates a TitleAreaDialog with four groups in them. Each of the groups contain controls and the last one is "dynamic", i.e. when selecting an entry in the combo box, a certain number of controls is generated.
Now, fortunately, the dialog re-sizes automatically -- but I'd like to have more control over the resize. As it is right now, the dialog size is "distributed" evenly between the groups. What I would like to accomplish is to only enlarge (or shrink) the last group, even if the user resizes the dialog with the mouse. The size (height) of the three static groups should remain fixed.
Is there a way to achieve this? TIA!
public class DialogExample extends ApplicationWindow {
class MyDialog extends TitleAreaDialog {
MyDialog(Shell shell) {
super(shell);
setShellStyle(getShellStyle() | SWT.MAX | SWT.RESIZE);
}
private Composite createComposite(Composite parent) {
Composite c = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
layout.horizontalSpacing = 10;
c.setLayout(layout);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
return c;
}
private Group createGroup(Composite parent, String title) {
Group newGroup = new Group(parent, SWT.NONE);
newGroup.setText(title);
newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
GridLayout layout = new GridLayout();
newGroup.setLayout(layout);
return newGroup;
}
private Group createGroup(Composite parent, int controlCount) {
Group newGroup = createGroup(parent, "Group " + controlCount);
for (int i = 0; i < controlCount; i++) {
Label l = new Label(newGroup, SWT.NONE);
l.setText("Label " + (i + 1));
}
return newGroup;
}
private Group createControlGroup(final Composite parent) {
final Group newGroup = createGroup(parent, "Control Group");
final Combo combo = new Combo(newGroup, SWT.READ_ONLY);
combo.add("1");
combo.add("2");
combo.add("3");
combo.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent event) {
for (Control c: newGroup.getChildren()) {
if (c != combo) {
c.dispose();
}
}
for (int i = 0; i <combo.getSelectionIndex() + 1; i++) {
Label l = new Label(newGroup, SWT.None);
l.setText("Dynamic Label " + (i + 1));
}
parent.getShell().pack();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
return newGroup;
}
public void create() {
super.create();
setTitle("Title");
setMessage("Message");
}
protected Control createDialogArea(Composite parent) {
Composite main = createComposite(parent);
createGroup(main, 1);
createGroup(main, 2);
createGroup(main, 3);
createControlGroup(main);
return main;
}
}
public DialogExample()
{
super(null);
}
public void run() {
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected Control createContents(Composite parent) {
Label label = new Label(parent, SWT.CENTER);
label.setText("Dialog example");
TitleAreaDialog dialog = new MyDialog(parent.getShell());
dialog.open();
return label;
}
public static void main(String... args) {
new DialogExample().run();
}
}
Upvotes: 2
Views: 1076
Reputation: 36894
The reason for that is that you use the following:
newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
which means that each group should stretch across the parent and occupy as much space as possible.
You can fix that by using:
newGroup.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
for the components that should not stretch vertically and:
newGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for those that should.
If you haven't read "Understanding Layouts in SWT" yet, please do so here.
Upvotes: 1