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

On Win32, disable parent window controls while modal dialog is being created.

XMLWordPrintable

    • tiger
    • x86
    • linux, windows_nt, windows_2000

      While a modal dialog is being created, the parent frame's controls (such as close, move, resize) are still active. To match Windows convention, it should be disabled on Windows. See the attached program below.

      If we have a dialog that brings out another modal dialog when clicking
      on a button, JDK will ensure that all the UI
      components in the first dialog cannot be activated while the second
      dialog is been constructed. However, it is still possible to press the
      Window's system close button ( i.e. the "X" button on the top right hand
      corner) of the first dialog. As a result, the first dialog gets
      dismissed before the second dialog is shown.

      ================================
      // TestDialog.java
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class TestDialog extends JDialog
      {
          JFrame frame;
          static int i = 0;
          public TestDialog( JFrame f, boolean wait )
          {
              super( f, true );
              frame = f;
              addWindowLisnener( new WindowAdapter() {
                  public void windowClosing( WindowEvent event )
                  {
                      System.out.println( "Disposing " + getTitle() );
                      setVisible( false );
                      dispose();
                  }
              });
              setTitle( "Dialog number " + ++i );
              setLocation( new Point( i * 20, i * 20 ) );
              setSize( new Dimension( 500, 200 ) );
              JButton btn2 = new JButton( "<html><center><strong>Press me to launch another modal dialog<br>that takes 5 seconds to contruct." );
              btn2.addActionListener( new ActionListener() {
                  public void actionPerformed( ActionEvent event )
                  {
                      TestDialog dlg2 = new TestDialog( frame, true );
                      dlg2.show();
                  }
              });
              JButton btn3 = new JButton( "<html><center><strong>You cannot press me when a modal dialog is being construction<br>but you can press the X button above." );
              btn3.addActionListener( new ActionListener() {
                  public void actionPerformed( ActionEvent event )
                  {
                      JOptionPane.showMessageDialog( frame, "You pressed me" );
                  }
              });
              JPanel panel = new JPanel( new BorderLayout() );
              panel.add( BorderLayout.NORTH, btn2 );
              panel.add( BorderLayout.SOUTH, btn3 );
              getContentPane().add( panel );

            if( wait )
              {
                  try
                  {
                      Thread.currentThread().sleep( 5000 );
                  }
                  catch( InterruptedException x )
                  {
                  }
              }
          }

          public static void main( String[] args )
          {
              final JFrame frame = new JFrame();
              frame.setSize( new Dimension( 500, 100 ) );
              JButton btn1 = new JButton( "<html><strong>Press me to launch a Modal Dialog" );
              btn1.addActionListener( new ActionListener()
              {
                  public void actionPerformed( ActionEvent event )
                  {
                      TestDialog dlg1 = new TestDialog( frame, false );
                      dlg1.show();
                  }
              });
              frame.getContentPane().add( btn1 );
              frame.show();
          }
      }


            son Oleg Sukhodolsky (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: