문제
Group group = new Group(parent, SWT.NONE);
StyledText comment = new StyledText(group, SWT.BORDER_DASH);
이것은 내부에 텍스트 영역이있는 그룹을 만듭니다.
나중에 텍스트를 삭제하려면 (텍스트를 다른 것으로 바꿀 수 있도록 화면에서 제거)?
해결책
위젯을 사용하십시오.
public class DisposeDemo {
private static void addControls(final Shell shell) {
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Click to remove all controls from shell");
button.addSelectionListener(new SelectionListener() {
@Override public void widgetDefaultSelected(SelectionEvent event) {}
@Override public void widgetSelected(SelectionEvent event) {
for (Control kid : shell.getChildren()) {
kid.dispose();
}
}
});
for (int i = 0; i < 5; i++) {
Label label = new Label(shell, SWT.NONE);
label.setText("Hello, World!");
}
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();
}
}
다른 팁
또 다른 옵션은 a를 사용하는 것입니다 stacklayout 기본 컨트롤 사이를 전환합니다. 이렇게하면 "위젯이 배치 된"오류가 발생하지 않습니다.
전화해야합니다 comment.changeParent(newParent)
또는 comment.setVisible(false)
그룹에서 제거/숨기기. 확실하지 않습니다 comment.changeParent(null)
효과가 있지만 시도해 볼 것입니다.
SWT가 사용하기 때문에 이런 식으로 수행합니다 복합 패턴.
group.getChildren()[0].dispose()
첫 번째 아이를 제거합니다. 삭제하려는 정확한 자녀를 식별하는 방법을 찾아야합니다. ID를 비교할 수 있습니다. 해당 컨트롤에서 setData / getData를 사용하여이를 수행 할 수 있습니다.
예를 들어:
StyledText comment = new StyledText(group, SWT.BORDER_DASH);
comment.setData("ID","commentEditBox");
그리고:
for (Control ctrl : group.getChildren()) {
if (control.getData("ID").equals("commentEditBox")) {
ctrl.dispose();
break;
}
}
제휴하지 않습니다 StackOverflow