Reteras Remus
Reteras Remus

Reputation: 933

JFace Dialog handle submitted data ( okPressed )

This is my dialog class: InputDialog which is opened with a button from another View. This Dialog contains a single Text input.

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

And this is how I open the dialog:

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

    }
});

How can I handle/read the returned or submitted value's from the Dialog?

Upvotes: 2

Views: 2809

Answers (1)

tkotisis
tkotisis

Reputation: 3552

You can persist the entered value in a field within the dialog, then use a getter after the dialog closes.

Since the InputDialog is blocking, you will have to check it's return value.

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

where

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

Upvotes: 6

Related Questions