如何处理一个小部件并创建另一个小部件来代替 SWT 中的小部件?
How to dispose a widget and create another widget in place of that in SWT?
我有一个组合,我在启动视图时创建了五个文本框。单击一个按钮,我想用组合框替换第 4 个文本框。问题是我无法从头重新加载页面,因为我想显示文本框的当前值。
这是一个片段。
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();
}
}
与其尝试替换控件(这很棘手),不如在开始时创建所有控件,但使其中一个控件不可见并将其从布局中排除。然后您可以稍后切换可见性。
创建时使用:
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);
要切换为显示组合:
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
我有一个组合,我在启动视图时创建了五个文本框。单击一个按钮,我想用组合框替换第 4 个文本框。问题是我无法从头重新加载页面,因为我想显示文本框的当前值。
这是一个片段。
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();
}
}
与其尝试替换控件(这很棘手),不如在开始时创建所有控件,但使其中一个控件不可见并将其从布局中排除。然后您可以稍后切换可见性。
创建时使用:
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);
要切换为显示组合:
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