How can I return a value from a JDialog box to the parent JFrame?

I generally do it like this:

Dialog dlg = new Dialog(this, ...);
Value result = dlg.showDialog();

The Dialog.showDialog() function looks like this:

ReturnValue showDialog() {
    setVisible(true);
    return result;
}

Since setting visibility to true on a JDialog is a modal operation, the OK button can set an instance variable (result) to the chosen result of the dialog (or null if canceled). After processing in the OK/Cancel button method, do this:

setVisible(false);
dispose();

to return control to the showDialog() function.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)