views:

68

answers:

2
// This is supposed to show a modal dialog and then hide it again. In practice,
// this works about 75% of the time, and the other 25% of the time, the dialog
// stays visible.
// This is on Ubuntu 10.10, running:
// OpenJDK Runtime Environment (IcedTea6 1.9) (6b20-1.9-0ubuntu1)

// This always prints
// setVisible(true) about to happen
// setVisible(false) about to happen
// setVisible(false) has just happened
// even when the dialog stays visible.

package modalproblemdemo;

import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;

public class Main {
    public static void main(String[] args) {
        final Dialogs d = new Dialogs();
        new Thread() {
            @Override
            public void run() {
                d.show();
                d.hide();
            }
        }.start();
    }

    static class Dialogs {
        final JDialog dialog;

        public Dialogs() {
            dialog = new JDialog((Frame) null, "Hello World", /*modal*/ true);
            dialog.setSize(400, 200);
        }

        public void show() {
            SwingUtilities.invokeLater(new Runnable() { public void run() {
                dialog.setLocationRelativeTo(null);
                System.out.println("setVisible(true) about to happen");
                dialog.setVisible(true);
            }});
        }

        public void hide() {
            SwingUtilities.invokeLater(new Runnable() { public void run() {
                System.out.println("setVisible(false) about to happen");
                dialog.setVisible(false);
                System.out.println("setVisible(false) has just happened");
            }});
        }
    }
}
+4  A: 

It is clearly some sort of race condition. I don't think it is as simple as Erick Robertson's answer. Dialog's show() code is quite complicated, it contains some special logic for being called from the event dispatch thread and it also posts events to the event queue. Maybe the order in which events are posted is somehow affected by thread delays.

Perhaps what you need is SwingUtilities.invokeAndWait(), this way you guarantee that setVisible(true) has finished execution before you call setVisible(false). As Skip Head pointed out, invokeAndWait will block until the dialog is closed.

And why do you need it anyways?

EDIT: This is my scenario of what's happening:

  1. you call d.show() that posts setVisible(true) event
  2. the thread is put so sleep by the scheduler and EDT kicks in and starts executing the first event
  3. EDT gets kicked out before the first task has finished and posted an actual event that shows the dialog
  4. your thread executes d.hide() that posts the setVisible(false) event. The thread is finished and EDT kicks in
  5. EDT finished the first task, puts its showing event to the event queue
  6. It goes to the next event, and, voila, it is the setVisible(false) event!
  7. It messes up the whole state of the dialog and it stays visible and unresponsive.

EDIT2: Looks like ProgressMonitor has the functionality you are trying to implement.

tulskiy
InvokeAndWait in show() causes the modal dialog to block waiting to be closed. I think this is what Zarkonnen is trying to avoid with all the extra threads.
Skip Head
@Skip Head: yeah, you're right.
tulskiy
I need it as a "please wait, processing" modal dialog, to tell the user the program is busy working. It needs to be modal to stop the user from clicking buttons while the program is busy ('cause then both the EDT and the worker thread would be manipulating state).
Zarkonnen
@Zarkonnen: and the event is sometimes very short and causes a race condition? Try to put both methods in a `synchronized` block, maybe it will work.
tulskiy
@Zarkonnen: see my last edit.
tulskiy
@tulskiy ProgressMonitor's window is actually not modal!
Zarkonnen
@Zarkonnen: have you tried the locks and synchronized?
tulskiy
+1  A: 

You could try to dispose() the dialog instead of hiding it, but that would require you to rebuild it if you wanted to show it again.

Qwerky