2

これは私のダイアログ クラスです: 別のビューからのボタンで開かれる 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

    }
});

ダイアログから返された値または送信された値を処理/読み取るにはどうすればよいですか?

4

1 に答える 1

6

入力した値をダイアログ内のフィールドに保持し、ダイアログを閉じた後に 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;
    }
}    
于 2012-12-03T12:04:18.323 に答える