Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4818193

FocusListener opening a modal dialog blocks whole application

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.1
    • client-libs
    • x86
    • windows_2000



      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)
      ======================================================================

            prssunw Prs Prs (Inactive)
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: