Reputation: 153
I have a composite where I create five textboxes on launching the view. There is a button on click of which I want to replace the 4th textbox with a combo box. The problem is I can't reload the page from the beginning as I want to display the current value of the textboxes.
Here is a snippet.
public class DisposeDemo {
private static void addControls(Shell shell) {
shell.setLayout(new GridLayout());
Text textOne=new Text(shell, SWT.BORDER);
Text textTwo=new Text(shell, SWT.BORDER);
Text textThree=new Text(shell, SWT.BORDER);
Text textFour=new Text(shell, SWT.BORDER);
Text textFive=new Text(shell, SWT.BORDER);
Button button = new Button(shell, SWT.PUSH);
button.setText("Replace text with Combo");
button.addSelectionListener(new SelectionAdapter() {
@Override public void widgetSelected(SelectionEvent event) {
//What code should go here to Replace the textFour with a combo
}
});
shell.pack();
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
addControls(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Upvotes: 0
Views: 807
Reputation: 111217
Rather than trying to replace a control (which is tricky) create all the controls at the start but make one of them invisible and exclude it from the layout. You can then switch the visibility later on.
When creating use:
Text textFour = new Text(shell, SWT.BORDER);
GridData data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
textFour.setLayoutData(textFour);
Combo comboFour = new Combo(shell, ... style ...);
comboFour.setVisible(false); // Not visible
data = new GridData(SWT.LEFT, SWT.CENTER, false, false);
data.exclude = true; // Exclude from layout
comboFour.setLayoutData(data);
To switch to have the combo visible:
textFour.setVisible(false);
GridData data = (GridData)textFour.getLayoutData();
data.exclude = true;
comboFour.setVisible(true);
data = (GridData)comboFour.getLayoutData();
data.exclude = false;
shell.layout(true); // Tell shell to redo the layout
Upvotes: 1