wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Hiding modal dialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hiding modal dialog" Watch "Hiding modal dialog" New topic

Hiding modal dialog

Marcin Mikosik

Joined: Dec 05, 2006
Posts: 2
I encounter the problem in the code that is pasted below and have no idea where the problem lies. The crucial part of the code is MyAction.actionPerformed method. It simply makes the modal-dialog visible (which blocks the thread) but on the previous line it starts the thread that will call the SwingUtilities.invokeLater with the Runnable that hides the dialog. This way the thread that shows the dialog will be blocked till the thread that calls invokeLater does his jobs and hides the dialog. All code runs in Swing-event-thread except the Thread that is created only invoke the SwingUtilities.invokeLater method.
The problem is that when you run the app and try to run the MyAction (it is plugged as GO item in the frame's menu) a few times you see that the dialog is not alway correctly hidden (someties I need 20 tries to see the effect).
Sometimes the bug manifest itself by not hiding the dialog (and additionaly the dialog is broken - either it does not have anything inside contentPane or it is just window-title-bar without any content below) sometimes the dialog is hidden but the frame does not give the focus back (as if the dialog was still displayed but was invisible).
Calling the dispose instead of setVisible(false) has the similar effect.
The problem looks like some kind of race, but how it could be if everything is done from the Swing-thread.
I observer the problem on sun-jdk- and ibm-jdk-
I would be grateful for any tips on that problem.


The code below:
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Main {

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame();
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("mainmenu");

Action action = new MyAction(frame);
action.putValue(Action.NAME, "GO");
JMenuItem item = new JMenuItem(action);


private static class ProgressShower {
private final JDialog progressDialog;
private State state;
private final Frame owner;

private static enum State {

public ProgressShower(Frame owner, String title, String message) {

this.owner = owner;
this.state = ProgressShower.State.NOT_YET_STARTED;
this.progressDialog = constructDialog(title, message);

private JDialog constructDialog(String title, String message) {
JPanel panel = new JPanel();
panel.add(new JLabel("This is a label"));
panel.add(new JLabel(message));

final JDialog dialog = new JDialog(this.owner, title, true);

return dialog;

public void start() {
if (this.state == ProgressShower.State.NOT_YET_STARTED) {
this.state = ProgressShower.State.SHOWING_PROGRESS;

public void stop() {
if (this.state == ProgressShower.State.SHOWING_PROGRESS) {
this.state = ProgressShower.State.STOPPED;

public static class ClosingThread extends Thread {
public void run() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {

public static class MyAction extends AbstractAction {
private final JFrame frame;
public MyAction(JFrame frame) {
this.frame = frame;
public void actionPerformed(ActionEvent event) {


final Thread th = new ClosingThread();


private static ProgressShower shower;
public static void initShower(JFrame frame) {
shower = new ProgressShower(frame, "dialog title", "dialog message");
public static void startShower() {
public static void stopShower() {
Marcin Mikosik

Joined: Dec 05, 2006
Posts: 2
The problem described above is now accepted by the sun as bug.
For details see:
Consider Paul's rocket mass heater.
subject: Hiding modal dialog
Similar Threads
modeless JDialog?
gui and thread
char[] security
Display JDialog Problem
java upgrade makes modal dialog lock up application