You can simply remove the Label
and put the new Text
in its place. GridLayout
uses z-order to determine the location in the grid, so you need to use moveAbove()
and moveBelow()
in Text
to get it in the right place. Then call layout()
on the parent. For instance:
Text text = new Text(label.getParent(), SWT.BORDER); text.moveAbove(label); label.dispose(); text.getParent().layout();
Here is a simple widget that shows exactly what I mean:
public class ReplaceWidgetComposite extends Composite { private Label label; private Text text; private Button button; public ReplaceWidgetComposite(Composite parent, int style) { super(parent, style); setLayout(new GridLayout(1, false)); label = new Label(this, SWT.NONE); label.setText("This is a label!"); button = new Button(this, SWT.PUSH); button.setText("Press me to change"); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { text = new Text(ReplaceWidgetComposite.this, SWT.BORDER); text.setText("Now it a text!"); text.moveAbove(label); label.dispose(); button.dispose(); ReplaceWidgetComposite.this.layout(true); } }); } }
source share