I am creating a simple SWT container in my Eclipse RCP application and was having an issue with the location/order in which it is displayed. Here is the code I am using.
@PostConstruct
public void createControls(Composite parent)
{
parent.setBackground(new Color (Display.getCurrent (), 255, 144, 0));
GridData parentData = new GridData(SWT.FILL, SWT.FILL, true, true);
parent.setLayout(new GridLayout(1, true));
parent.setLayoutData(parentData);
Device device = Display.getCurrent ();
Color backgroundColor = parent.getBackground();
Color whiteColor = new Color (device, 255, 255, 255);
Color randomColor = new Color (device, 255, 0, 0);
final Composite criteriaContainer = new Composite(parent, SWT.BORDER);
criteriaContainer.setBackground(randomColor);
final GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
final GridData comboGridData = new GridData(SWT.FILL, SWT.TOP, true, false,1,1);
criteriaContainer.setLayout(new GridLayout(1, false));
criteriaContainer.setLayoutData(gridData);
final Label primaryComboLabel = new Label(criteriaContainer, SWT.NONE);
primaryComboLabel.setLayoutData(comboGridData);
primaryComboLabel.setForeground(whiteColor);
primaryComboLabel.setText("View by:");
criteriaContainer.layout();
criteriaContainer.pack();
parent.layout();
parent.pack();
}
I cant seem to get the label to appear at the top of the application(appears at the bottom center)[enter image description here][1]. If I write the same code and execute it as a standalone SWT application, the label appears at the top left. [1]: https://i.sstatic.net/OL312.png Here is the difference when I have a standalone swt app.
public void showHistoryContainer() throws Exception {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setBackground(new Color (Display.getCurrent (), 255, 144, 0));
the rest of the code is the same. [2]: https://i.sstatic.net/fJmR6.png
Any ideas why this may be happening ?
Note: In my RCP application, I am not doing any other processing on my parent composite.
You don't say what the createControls
is a part of. Is it an MPart
?
I can't really reproduce this problem but there are a number of issues in your code which might be causing it.
Never change the Layout of a Composite
passed to your code (parent
here). Create another Composite
as a child and set the layout on that.
Don't call layout
and pack
.
So simplifying your code I have:
@PostConstruct
public void postConstruct(Composite parent)
{
Display device = parent.getDisplay();
Composite body = new Composite(parent, SWT.NONE);
body.setBackground(new Color(device, 255, 144, 0));
body.setLayout(new GridLayout());
Color whiteColor = new Color(device, 255, 255, 255);
Color randomColor = new Color(device, 255, 0, 0);
Composite criteriaContainer = new Composite(body, SWT.BORDER);
criteriaContainer.setBackground(randomColor);
criteriaContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
criteriaContainer.setLayout(new GridLayout());
Label primaryComboLabel = new Label(criteriaContainer, SWT.NONE);
primaryComboLabel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
primaryComboLabel.setForeground(whiteColor);
primaryComboLabel.setText("View by:");
}
Finally if you create Color
objects like this you must arrange to dispose of them or you will leak resources. If this is an e4 RCP you should use the CSS support instead.