SWT复合 – 重绘问题

我有一个复合元素,最初有一个Label。 现在我调用dispose(标签)并在同一个容器(复合榆树)中创建另一个标签,但我没有看到新文本。 它让我质疑如何在复合上启用重绘,以便新标签(或我可能创建的任何其他组件)将代替旧标签。 这是我的代码(分为unit testing重绘复合)

private Label createLabel( Composite parent) { Label label = new Label(parent, SWT.NONE); label.setAlignment(SWT.CENTER); label.setLayoutData( new GridData( SWT.CENTER, SWT.CENTER, true, true) ); return label; } private void changeText() { assert testCell != null : "Please initialize test cell"; testCell.getChildren()[0].dispose(); Label l = createLabel(testCell); l.setText("New TexT"); testCell.redraw(); } private void draw() { Display display = new Display(); shell = new Shell(display); shell.setLayout(new GridLayout(2,false)); testCell = new Composite(shell,SWT.BORDER); testCell.setLayout(new GridLayout()); Label l = createLabel(testCell); l.setText("Old Text"); Composite btnCell = new Composite(shell,SWT.NONE); btnCell.setLayout(new GridLayout()); Button b = new Button(btnCell, SWT.PUSH); b.setText("Change"); b.addListener(SWT.MouseDown, new Listener() { public void handleEvent(Event e) { changeText(); } }); 

如您所见,我在添加新元素后在复合上调用redraw。 此外,我已经validation在调用dispose之后,testCell.getChildren()。length按预期返回0,当我创建一个新标签时,我得到相同的表达式返回1,validation新元素确实得到了添加到其父复合容器我错过了什么吗?

changeText()函数中

testCell.redraw();

行应该被替换

testCell.layout();

或者,如果要正确resize,则应使用

shell.layout();

我想说在标签上添加一个selectionListener。

 .addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(final SelectionEvent e) { //Change text by Label.setText(); } }