【问题标题】:SWT/JFace: remove widgetsSWT/JFace:删除小部件
【发布时间】:2009-04-01 07:55:41
【问题描述】:
Group group = new Group(parent, SWT.NONE);
StyledText comment = new StyledText(group, SWT.BORDER_DASH);

这将创建一个组,其中包含一个文本区域。

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

【问题讨论】:

    标签: java swt jface


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您必须致电 comment.changeParent(newParent)comment.setVisible(false) 将其从群组中移除/隐藏。我不确定comment.changeParent(null) 是否可行,但我会尝试一下。

        我们这样做是因为 SWT 使用 Composite Pattern

        【讨论】:

          【解决方案4】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-17
            • 2011-12-28
            相关资源
            最近更新 更多