2016-08-03 58 views
0

我有一个Composite的子类,创建了N个组合框,其中N由输入定义。所以当用户进行更改时,组合框的数量可能会发生变化。目前,这不会发生。我试着做这两种方式:swt - 重新创建复合

// On event, straight reconstruct, no change to number of dropdowns 
myComp = new MyComposite(parent, SWT.NONE, newNum); 

// On event, dispose and reconstruct, this completely removes my composite from the gui 
myComp.dispose(); 
myComp = new MyComposite(parent, SWT.NONE, newNum); 

这里是我的综合类:

public MyComposite(Composite parent, int num) { 
    super(parent, SWT.BORDER); 
    this.setText("MY Composite"); 
    this.setLayout(new GridLayout(1, true)); 
    this.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); 

    combos = new HashMap<>(); 
    for(int i = 0; i < num; i++) { 
     combos.put(i, new Combo(this, SWT.NONE)); 
    } 
} 

是否有其他/更好的方式来做到这一点?

回答

2

如果您更改Composite的内容,则需要告诉它再次布置其内容。

所以经过

myComp.dispose(); 
myComp = new MyComposite(parent, SWT.NONE, newNum); 

你需要做的

parent.layout(true, true);