JDK-6518198 : Closing modal JDialog does not return focus to JFrame
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: linux
  • CPU: x86
  • Submitted: 2007-01-26
  • Updated: 2012-08-28
  • Resolved: 2012-08-28
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_10"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_10-b03)
Java HotSpot(TM) Client VM (build 1.5.0_10-b03, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux gentoo 2.6.18-gentoo-r3 #1 PREEMPT Thu Nov 30 07:46:37 UTC 2006 i686 Intel(R) Celeron(R) CPU 2.40GHz GenuineIntel GNU/Linux

EXTRA RELEVANT SYSTEM CONFIGURATION :
KDE 3.5.x

A DESCRIPTION OF THE PROBLEM :
When I show and hide the JDialog from EventDispatching Thread the focus does not return to the main JFrame as if it was stolen by the JDialog. To reproduce the bug it is necessary to invoke the setVisible(tru) and setVisible(false) on the JDialog from the EventDispatchingThread by passing two different Runnables to SwingUtilities.invokeLater() - first Runnable shows the JDialog, the second one hides. The source code for reproducing the bug in "Source code for an executable test case" section.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
  To reproduce the bug run the following code. It shows the JFrame with one button. Pressing the button will show and hide the Jdialog. Press a button a few times and you will see that  after one of the presses the JFrame loses the focus. It will not be possible to press the button on the JFrame using spacebar.
On my machine I need to press the button approximately 15-20 times to see the JFrame without focus.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Pressing the button (thus showing and hiding the JDialog) should not affect the state of the JFrame. After the JDialog is hidden the JFrame should have the focus.
ACTUAL -
After a few button presses (about 20) the JFrame will lost the focus forever.

REPRODUCIBILITY :
This bug can be reproduced occasionally.

---------- BEGIN SOURCE ----------

import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
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();
                Action action = new MyAction();
                action.putValue(Action.NAME, "GO");
                frame.getContentPane().add(new JButton(action));
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                frame.setSize(200,100);
                frame.setVisible(true);
            }
        });
    }
    public static class MyAction extends AbstractAction {
        public void actionPerformed(ActionEvent event) {
            new TestIt().testDialog();
        }
    }
    public static class TestIt {
        private JDialog dialog;
        private void show() {
            JPanel panel = new JPanel();
            panel.add(new JLabel("This is a label"));
            panel.add(new JLabel("message"));
            
            this.dialog = new JDialog((Frame) null, "title", true);
            this.dialog.getContentPane().add(panel);
            this.dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
            this.dialog.setLocationRelativeTo(null);
            this.dialog.pack();
            this.dialog.setVisible(true);
            this.dialog.dispose();
        }
        private void hide() {
            this.dialog.setVisible(false);
        }
        public void testDialog() {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    show();
                }
            });
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    hide();
                }
            });
        }
    }
}

---------- END SOURCE ----------

Comments
EVALUATION This problem is only reproducible with 1.5.0_xx on KDE, and not reproducible with 6.0 on any desktop manager.
29-01-2007