Question

I am using a FormLayout for a Composite container. When I add two children label and clientArea, where the clientArea depends on label - the clientArea only appears, when I add the label first.

Calling layout() on the container, after adding the children doesn't help - the clientArea doesn't show up.

How can I add children to a FormLayout-controlled container, independant from their dependencies from each other?

MyLabel label;
Composite clientArea;   

public MyContainer(Composite parent, int style) {
    super(parent,style);

    //choose the container Layout
    FormLayout layout = new FormLayout();
    this.setLayout(layout);


    clientArea = new Composite(this, SWT.NONE);
    FormData formData4ClientArea = new FormData();
    formData4ClientArea.left = new FormAttachment(0,0);
    formData4ClientArea.top = new FormAttachment(0,5);
    formData4ClientArea.right = new FormAttachment(label,-5);
    formData4ClientArea.bottom = new FormAttachment(100,-5);
    //set the Formdata
    clientArea.setLayoutData(formData4ClientArea);
    clientArea.setBackground(getDisplay().getSystemColor(SWT.COLOR_GREEN));     


    //create the label
    label = new MyLabel(this, SWT.NONE);
    FormData formData4Label = new FormData();
    formData4Label.top = new FormAttachment(0,5);
    formData4Label.right = new FormAttachment(100,-5);
    formData4Label.bottom = new FormAttachment(100,-5);
    //set the FormData
    label.setLayoutData(formData4Label);
Was it helpful?

Solution

formData4ClientArea.right = new FormAttachment(label,-5); At this point, label is null. It is not instantiated. So basically you attach the clientArea to nothing. If you want clientArea to be attached to label, ofc you need to instantiate label first, then clientArea

But,on the other hand, why does the order matter to you?

OTHER TIPS

In fact, the order in which you instantiate the components doesn't matter.

What you cannot do is reference an object before its creation. As Plygnome stated, the problem is that when you create the FormAttachment, the Label is null.

On our projects, we first create all the components and then we create all their layout data objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top