How to replace grid cell contents in SWT GridLayout?

I need to replace the contents of a grid cell after clicking a button. For example: there is a Label, and I need to replace it with text. Is this possible with GridLayout? I need to use SWT.

+6
source share
1 answer

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); } }); } } 
+8
source

Source: https://habr.com/ru/post/898673/


All Articles