swt - 重新创建复合

swt - Recreate a composite

我有一个 Composite 的子class,它创建 N 个组合框,其中 N 由输入定义。因此,当用户进行更改时,组合框的数量可能会发生变化。目前,这不会发生。我尝试了两种方法:

// 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);

这是我的合成图 class:

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));
    }
}

有another/better方法吗?

如果您更改 Composite 的内容,您需要告诉它您重新布局其内容。

所以在

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

你需要做的

parent.layout(true, true);