非常easy的东西,就是先dispose再create。
写出来的主要是看看大家有没有更好的实现方法
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* 该程序目的:通过事件驱动,简单演示动态创建组件。
* 选择“是”的时候:供选择的是下拉框
* 选择“否”的时候:是进行具体的文本配置
* @author Administrator
*/
public class MyComposite {
private Button useOutSourceButton;
private Button newSourceButton;
private Composite dbSourceComposite;
private Shell shell;
/**
* 主程序
* @param args
*/
public static void main(String[] args) {
MyComposite myComposite = new MyComposite();
myComposite.open();
}
/**
* shell
*/
private void open() {
Display display = Display.getDefault();
shell = new Shell(display);
shell.setText("组件替换测试");
shell.setSize(400, 200);
shell.setLayout(new GridLayout(3, false));
final Label label = new Label(shell, SWT.NONE);
label.setLayoutData(new GridData(100, SWT.DEFAULT));
label.setText("使用数据库资源");
useOutSourceButton = new Button(shell, SWT.RADIO);
useOutSourceButton.setText("是");
newSourceButton = new Button(shell, SWT.RADIO);
newSourceButton.setText("否");
dbSourceComposite = new Composite(shell, SWT.NONE);
dbSourceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,true, false,3,1));
dbSourceComposite.setLayout(new GridLayout(3,false));
createInnerListeners();
shell.open();
while (!display.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* 事件监听器
*/
protected void createInnerListeners() {
useOutSourceButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getSelection()) {
initSelectDBSourceUI(dbSourceComposite);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
newSourceButton.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button btn = (Button) e.getSource();
if (btn.getSelection()) {
initConfigDBSourceUI(dbSourceComposite);
}
}
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
/**
* 创建部件组1
* @param composite
*/
private void initConfigDBSourceUI(final Composite composite) {
this.disposedComposite(composite);
Text configureText = new Text(composite, SWT.BORDER);
configureText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
false));
configureText.setEditable(false);
Button configureButton = new Button(composite, SWT.NONE);
configureButton.setText("配置");
Button resetButton = new Button(composite, SWT.NONE);
resetButton.setText("重置");
shell.layout(true, true);
}
/**
* 创建部件组2
* @param composite
*/
private void initSelectDBSourceUI(final Composite composite) {
this.disposedComposite(composite);
Label dbTypeLabel = new Label(composite, SWT.NONE);
dbTypeLabel.setText("数据库资源名称");
dbTypeLabel.setLayoutData(new GridData(100, SWT.DEFAULT));
dbTypeLabel.setToolTipText("选择已有数据库资源");
CCombo dbCombo = new CCombo(composite, SWT.BORDER);
dbCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
dbCombo.setEditable(false);
Button resetButton = new Button(composite, SWT.NONE);
resetButton.setText("重置");
shell.layout(true, true);
}
/**
* 销毁原有部件
* @param composite
*/
private void disposedComposite(Composite composite) {
Control[] arrays = composite.getChildren();
for (Control control : arrays) {
if (control != null)
control.dispose();
}
}
}
效果图: