5

I see the following code in a Java/Swing GUI project:

MyDialog dlg = new MyDialog(parent, isFizz);
MyDialogResults results = dlg.getResults();

eventBus.fireEvent(new MyDialogEvent(results));

In the code above, MyDialog extends JDialog. So clearly, a child dialog is being constructed (and somehow shown to the end user), and then when the user exits the dialog (by clicking OK or some other button), a results bean is used to fire a new event on the event bus.

But I'm struggling with two things:

  1. How/where is Java being told to actually draw/show the dialog to the user?; and
  2. How/where is Java being told to hang/wait for the user to exit the dialog, before firing the event to the bus?

Is it that a JDialog child will always show the dialog when the JDialog is created, and not return from the constructor until the dialog is exited? Here's the synopsis of hte MyDialog constructor from above:

public class MyDialog extends JDialog {
    private boolean isFizz;
    private MyDialogResults results;

    // Getters and setters for all properties...

    public MyDialog(Frame parent, boolean isFizz) {
        super(parent, "My Dialog", true);

        setIsFizz(isFizz);

        setVisible(true);
        dispose();
    }
}

Thanks in advance for any help/insight with understanding how the dialog opens, "hangs" (although I know its not actually hung/frozen), closes, and then fires the event.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

1

1)

setVisible(true);

2)

super(parent, "My Dialog", true);

Read the JavaDocs for JDialog for more details.

Most relevant are:

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks @Andrew Thompson (+1) - however for #2 I'm still confused. According to the [constructor Javadoc](http://docs.oracle.com/javase/7/docs/api/javax/swing/JDialog.html#JDialog%28java.awt.Dialog,%20java.lang.String,%20boolean%29) setting `modal` to true only "blocks user input to other top-level windows when shown". But that's not the same as what I'm describing here. I don't believe that would prevent the next line of code (`MyDialogResults results = dlg.getResults();`) from executing until the constructor finished, as that isn't "user input". –  Apr 29 '13 at 15:23
  • 1
    The "true" in the super call makes the dialog modal. That means that the call to `setVisible(true)` will not return until the dialog is closed. – Russell Zahniser Apr 29 '13 at 15:25
  • *""blocks user input to other top-level windows when shown"* ..and stops the next code line from executing (is what should be added to that). – Andrew Thompson Apr 29 '13 at 15:26
  • Thanks @RussellZahniser (+1) - that was the last piece of the puzzle that I needed! –  Apr 29 '13 at 15:26
1

Dialog.setVisible() is the method that shows the dialog and blocks until the dialog is closed. See the documentation for Dialog. dispose() then destroys the dialog object.

So your guess was partially correct. The constructor is showing the dialog, blocking until it is closed, and then destroying the dialog afterward. However, this is a feature of "MyDialog," not of JDialog

Reyan
  • 584
  • 1
  • 5
  • 16
  • The dialog is not destroyed until the dlg instance goes out of scope. That's why you can retrieve values from the dialog after the user presses the OK button. There ought to be a check for the Cancel button after the dialog constructor line as well. – Gilbert Le Blanc Apr 29 '13 at 15:32