Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Swing dialogs even work?

If you open a dialog in Swing, for example a JFileChooser, it goes somewhat like this pseudocode:

swing event thread {
  create dialog
  add listener to dialog close event {
    returnValue = somethingFromDialog
  }
  show dialog
  (wait until it is closed)
  return returnValue
}

My question is: how can this possibly work? As you can see the thread waits to return until the dialog is closed. This means the Swing event thread is blocked. Yet, one can interact with the dialog, which AFAIK requires this thread to run.

So how does that work?

like image 731
Bart van Heukelom Avatar asked Jun 12 '10 13:06

Bart van Heukelom


People also ask

What is a dialog in swing?

They are based on the JDialog class. Standard dialogs are predefined dialogs available in the Swing toolkit, for example the JColorChooser or the JFileChooser . These are dialogs for common programming tasks like showing text, receiving input, loading and saving files.

What are the 4 JOptionPane dialog boxes?

You can use a custom icon, no icon at all, or any one of four standard JOptionPane icons (question, information, warning, and error).

When a dialogue box is active it blocks user input to all other windows in the program?

Modal dialog box — A dialog box that blocks input to some other top-level windows in the application, except for windows created with the dialog box as their owner. The modal dialog box captures the window focus until it is closed, usually in response to a button press.

What is the difference between JFrame and JDialog?

JFrame is a normal window with its normal buttons (optionally) and decorations. JDialog on the other side does not have a maximize and minimize buttons and usually are created with JOptionPane static methods, and are better fit to make them modal (they block other components until they are closed).


1 Answers

The existing event dispatch thread is blocked, and so swing creates another thread that pumps the events. This is then the event dispatch thread for the duration of the dialog.

Swing creates a separate native thread for pumping native OS window messages. This is separate from the AWT event thread.

On Windows, you see these threads

  "AWT-Windows"   - the native UI thread
  "AWT-EventQueue-0" - the current AWT event dispatch thread

EDIT: The downvote is correct. This is not true, at least not in all cases.

Modal dialogs often take care of pumping AWT events themselves. If you run the code

SwingUtilities.invokeAndWait(new Runnable()
{
    public void run()
    {
        JOptionPane.showInputDialog("hello");
    }
});

and then break, looking at the threads, you will see only one EventQueue thread. The show() method of JOptionPane pumps events itself.

Frameworks like Spin and Foxtrot take the same approach - they allow you to create a long running blocking method on the EDT, but keep the events flowing by pumping events themselves. It is possible for swing to have multiple dispatch threads (I'm sure this was the case with older versions of swing) but now that multicore is common, the concurrency issues, in particular ensuring changes on one thread are correctly published to other threads, mean that using multiple EDTs produces bugs in the current implementation. See Multiple Swing event-dispatch threads

like image 143
mdma Avatar answered Sep 22 '22 01:09

mdma