JFace 对话框处理提交的数据( okPress)



这是我的对话框类:InputDialog,它是用另一个视图中的按钮打开的。此对话框包含单个文本输入。

public class InputDialog extends Dialog{
   public InputDialog(Shell parentShell) {
      super(parentShell);
      // TODO Auto-generated constructor stub
   }
   @Override
   protected Control createDialogArea(Composite parent) {
       parent.setLayout(new GridLayout(1, false));
       Text txtName = new Text(parent, SWT.NONE);
       return super.createDialogArea(parent);
   }
   @Override
   protected void okPressed() {
       // TODO Auto-generated method stub
       super.okPressed();
   }
}

这就是我打开对话框的方式:

buttAdd.addSelectionListener(new SelectionListener() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        // TODO Auto-generated method stub
        InputDialog dialog = new InputDialog(new Shell());
        dialog.open();
    }
    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
        // TODO Auto-generated method stub
    }
});

如何处理/读取对话框中返回或提交的值?

您可以将输入的值保留在对话框内的字段中,然后在对话框关闭后使用 getter。

由于InputDialog阻塞,因此您必须检查其返回值。

if (Window.OK == dialog.open()) {
    dialog.getEnteredText();
}

哪里

public class InputDialog extends Dialog {
    private Text txtName;
    private String value;
    public InputDialog(Shell parentShell) {
        super(parentShell);
        value = "";
    }
    @Override
    protected Control createDialogArea(Composite parent) {
        parent.setLayout(new GridLayout(1, false));
        txtName = new Text(parent, SWT.NONE);
        return super.createDialogArea(parent);
    }
    @Override
    protected void okPressed() {
        value = txtName.getText();
    }
    public String getEnteredText() {
        return value;
    }
}    

最新更新