java SWT/JFace:删除小部件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/704486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:26:44  来源:igfitidea点击:

SWT/JFace: remove widgets

javaswtjface

提问by Thilo

Group group = new Group(parent, SWT.NONE);
StyledText comment = new StyledText(group, SWT.BORDER_DASH);

This creates a group with a text area inside.

这将创建一个内部带有文本区域的组。

How can I later delete the text (remove it from the screen so that I can replace it with something else)?

我以后如何删除文本(从屏幕上删除它以便我可以用其他东西替换它)?

采纳答案by McDowell

Use Widget.dispose.

使用 Widget.dispose。

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

回答by James Van Huis

Another option is to use a StackLayoutto switch between underlying controls. This prevents you from running into a "widget is disposed" error.

另一种选择是使用StackLayout在底层控件之间切换。这可以防止您遇到“小部件已处理”错误。

回答by daanish.rumani

You have to either call comment.changeParent(newParent)or comment.setVisible(false)to remove/hide it from the Group. I am unsure if comment.changeParent(null)would work but I would give that a try.

您必须呼叫comment.changeParent(newParent)comment.setVisible(false)从组中删除/隐藏它。我不确定是否comment.changeParent(null)可行,但我会尝试一下。

We do it this way because SWT uses the Composite Pattern.

我们这样做是因为 SWT 使用复合模式

回答by ACV

group.getChildren()[0].dispose()will remove the first child. You need to find a way to identify the precise child you want to delete. It could be comparing the id. You can do that by using the setData / getData on that control:

group.getChildren()[0].dispose()将删除第一个孩子。您需要找到一种方法来确定要删除的确切子项。它可能是比较 id。您可以通过在该控件上使用 setData / getData 来做到这一点:

For example:

例如:

StyledText comment = new StyledText(group, SWT.BORDER_DASH);
comment.setData("ID","commentEditBox");

and then:

接着:

for (Control ctrl : group.getChildren()) {
 if (control.getData("ID").equals("commentEditBox")) {
   ctrl.dispose();
   break;
 }
}