JDK-4818193 : FocusListener opening a modal dialog blocks whole application
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-02-13
  • Updated: 2003-02-14
  • Resolved: 2003-02-14
Related Reports
Duplicate :  
Description

Name: jl125535			Date: 02/13/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]


A DESCRIPTION OF THE PROBLEM :
I have a textfield with a FocusListener added.
This listener opens a modal dialog if the textfield loses
the focus. Additionally I've got a JButton that itself
opens a modal dialog too. Its important to notice that this
button is not focusable.

If the textfield is focused and the mentioned button is
clicked, both dialogs are opened and the whole application
is blocked (with no chance to close the dialogs).

Here is a simple program that demonstrate this fact.
It has two buttons (the left one is focusable, the middle
one is not). Both buttons open an empty modal dialog if
clicked.
On the right side there's a textfield with a FocusListener
added that opens another (empty) modal dialog.


If the textfield is focused and the focusable button is
clicked only the dialog shown from the FocusListener
appears (where is the dialog from the button).

If the textfield is focused and the non-focusable button is
clicked two dialogs are shown and the system hangs.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Execute the sample program below


EXPECTED VERSUS ACTUAL BEHAVIOR :
When the textfield is focused and the (non-focusable)
button is clicked, the action from the FocusListener should
be processed fully and afterwards the action from the
button should be executed.

Instead it seems that the both modal dialogs are opened
(maybe a race condition) and the block eachother.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error messages, the application hangs, no dialog can be closed.

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;

public class Test extends JFrame
{
    
    public Test()
    {
        /*
         * A "normal" button that opens a modal dialog if clicked.
         * Note that its focusable.
         */
        JButton b1 = new JButton ( "keep focus" ) ;
        b1.addActionListener ( new ActionListener()
        {
            public void actionPerformed ( ActionEvent event )
            {
                JDialog dlg = new JDialog ( Test.this , "ActionListener (keep
focus)" , true ) ;
                dlg.setLocation ( 50 , 50 ) ;
                dlg.setSize ( 250 , 100 ) ;
                dlg.setDefaultCloseOperation ( JDialog.DISPOSE_ON_CLOSE ) ;
                dlg.setVisible ( true ) ;
            }
        } ) ;
        
        /*
         * Does the same as the button created before.
         * The only difference between the two buttons is the fact that this
one is NOT focusable.
         */
        JButton button = new JButton ( "not focusable" ) ;
        button.setFocusable ( false ) ;
        button.addActionListener ( new ActionListener()
        {
            public void actionPerformed ( ActionEvent event )
            {
                JDialog dlg = new JDialog ( Test.this , "ActionListener (not
focusable)" , true ) ;
                dlg.setLocation ( 200 , 200 ) ;
                dlg.setSize ( 250 , 100 ) ;
                dlg.setDefaultCloseOperation ( JDialog.DISPOSE_ON_CLOSE ) ;
                dlg.setVisible ( true ) ;
            }
        } ) ;
        
        /*
         * A simple textfield with a FocusListener.
         * If the focus is removed from the field the FocusListener opens a
modal dialog.
         */
        JTextField textfield = new JTextField ( 10 ) ;
        textfield.addFocusListener ( new FocusAdapter()
        {
            public void focusLost ( FocusEvent event )
            {
                JDialog dlg = new JDialog ( Test.this , "modal from
FocusListener" , true ) ;
                dlg.setLocation ( 400 , 400 ) ;
                dlg.setSize ( 250 , 100 ) ;
                dlg.setDefaultCloseOperation ( JDialog.DISPOSE_ON_CLOSE ) ;
                dlg.setVisible ( true ) ;
            }
        } ) ;
        
        getContentPane().setLayout ( new GridLayout ( 1 , 1 ) ) ;
        getContentPane().add ( b1 ) ;
        getContentPane().add ( button ) ;
        getContentPane().add ( textfield ) ;
        
        pack() ;
    }

    /**
     * Create a new <code>Test</code> instance and display it.
     */
    public static void main ( String[] args )
    {
        Test test = new Test() ;
        test.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ) ;
        test.setLocation ( 300 , 300 ) ;
        test.setVisible ( true ) ;
    }
    
}

---------- END SOURCE ----------
(Review ID: 144527) 
======================================================================

Comments
EVALUATION Name: rpR10076 Date: 02/14/2003 ###@###.### This is a duplicate of 4531693 which was fixed in Mantis along with a number of its duplicates. I have verified that the test doesn't hang on latest Mantis builds. ======================================================================
11-06-2004