Reputation: 1138
This is what I am trying to achieve, using SWT:
For this, I am trying to use RowLayout
for a nested Composite
, to wrap the composite's controls depending on the available space. The following code works perfectly:
public class RowLayoutExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT RowLayout test");
shell.setLayout(new RowLayout(SWT.HORIZONTAL));
for (int i = 0; i < 10; i++) {
new Label(shell, SWT.NONE).setText("Label " + i);
}
shell.setSize(400, 250);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
This is displayed (please notice the nice wrap of the last labels on the next line - also, on shell resize, the components wraps into available horizontal space):
When I do this, instead:
public class RowLayoutExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT RowLayout test");
shell.setLayout(new RowLayout(SWT.HORIZONTAL));
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new RowLayout(SWT.HORIZONTAL));
for (int i = 0; i < 10; i++) {
new Label(comp, SWT.NONE).setText("Label " + i);
}
shell.setSize(400, 250);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
I got the following behaviour. If I resize the shell, the labels are not wrapping into multiple lines.
In the below picture, we can see that the composite expands out of the shell client's area, instead of wrapping into second row. Resizing the shell doesn't affect this wrong behaviour.
I am using the following SWT version:
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.cocoa.macosx.x86_64</artifactId>
<version>4.3</version>
</dependency>
So, why the second scenario is not working? Furthermore, is it possible to use GridLayout
for shell, but RowLayout
for a child of this shell?
Upvotes: 2
Views: 530
Reputation: 36904
Here's an example using a GridLayout
as the Shell
's layout:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SWT RowLayout test");
shell.setLayout(new GridLayout());
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new RowLayout(SWT.HORIZONTAL));
comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for (int i = 0; i < 10; i++) {
new Label(comp, SWT.NONE).setText("Label " + i);
}
shell.setSize(400, 250);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Produces the same result as your first example.
The "trick" is to set the GridData
to the child of the element with the GridLayout
.
Upvotes: 4