Given the following situation:
// Initializing a new composite within a parent UI container
Composite composite = new Composite(parent, SWT.NONE);
Label label = new Label(composite, SWT.NONE);
label.setText("Hi. I am a label and I'm drawn correctly.");
Display.getDefault().syncExec(new Runnable() {
// Here I'm trying to draw a new label onto this composite
Label newLabel = new Label(composite, SWT.NONE);
Label.setText("I am a test label. You should see me now.");
// Change the text of 'label' here
label.setText("Uh-oh. My text has been altered.");
// Let's redraw the parent UI component to see the new label drawn
parent.redraw();
});
newLabel is never drawn, although label‘s text has been visually altered. Likewise, disposing an UI element within syncExec() consequently leads to its visual deletion. Why is that?
I cannot see a valid reason why it is impossible to do.
I don’t know if that’s the only problem but you have to at least relayout the composite and its children. Otherwise the label will just have a size of 0 and will therefore not be visible. You typically do this by calling
composite.layout(). Unless the composite doesn’t have a layout manager; then you would have to manually set its bounds.