JDK-5090034 : MODAL DIALOG THAT CONTAINS FOCUSABLE COMPONENTS BEHAVES WITH NO MODAL BLOCKING
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-08-20
  • Updated: 2005-06-03
  • Resolved: 2005-06-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6Resolved
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Name: rv122619			Date: 08/20/2004

If you have a modal dialog that contains focusable components (i.e. a message box using JOptionPane) that is displayed, you can use alt+tab to navigate to the parent window.  The parent window becomes active.  If you were to try and click on the parent window, the message box is re-activated.  Instead of clicking on the parent window if you were to use the arrow keys you could navigate the menu or if you were to use the accelerators you could perform the associated actions getting around the modal blocking.  

import java.awt.BorderLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
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.KeyStroke;
import javax.swing.WindowConstants;

public class TestFrame
         extends JFrame
{
   public static void main(String[] args)
   {
      boolean awtMenuBar = (args != null && args.length > 0 && args[0].equals("awt"));
      TestFrame frame = new TestFrame(awtMenuBar);
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
   
   public TestFrame(boolean useAWTMenuBar)
   {
      setTitle("Test Frame");
      getContentPane().add(new JLabel("Test Frame"));
      
      if (useAWTMenuBar)
         setMenuBar(createAWTMenuBar());
      else
         setJMenuBar(createJMenuBar());
   }

   private MenuBar createAWTMenuBar()
   {
      MenuBar menuBar = new MenuBar();
      Menu menu = new Menu("File");
      menuBar.add(menu);
      
      MenuItem mi = new MenuItem("Show Focusable Dialog");
      mi.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
      mi.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            showTestDialog(TestFrame.this, true);
         }
      });
      menu.add(mi);
      
      mi = new MenuItem("Show Non-Focusable Dialog");
      mi.setShortcut(new MenuShortcut(KeyEvent.VK_D, false));
      mi.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            showTestDialog(TestFrame.this, false);
         }
      });
      menu.add(mi);
      return menuBar;
   }

   private JMenuBar createJMenuBar()
   {
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("File");
      menuBar.add(menu);
      
      JMenuItem mi = new JMenuItem("Show Focusable Dialog");
      mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
      mi.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            showTestDialog(TestFrame.this, true);
         }
      });
      menu.add(mi);
      
      mi = new JMenuItem("Show Non-Focusable Dialog");
      mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
      mi.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            showTestDialog(TestFrame.this, false);
         }
      });
      menu.add(mi);
      
      return menuBar;
   }

   public static void showTestDialog(JFrame frame, boolean addFocusableComp)
   {
      final JDialog dialog = new JDialog(frame, "Test Dialog", true);
      dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      if (addFocusableComp)
      {
         dialog.getContentPane().add(new JLabel("Test dialog with a focusable component."), BorderLayout.NORTH);
         dialog.getContentPane().add(new JButton("A Focusable Button"));
      }
      else
      {
         dialog.getContentPane().add(new JLabel("Test dialog with no focusable components."));

//         // workaround 1
//         SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//               dialog.getContentPane().requestFocus();
//            }
//         });

//         // workaround 2
//         dialog.addKeyListener(new java.awt.event.KeyAdapter() {
//            public void keyPressed(java.awt.event.KeyEvent event) {
//               event.consume();
//            }
//            public void keyReleased(java.awt.event.KeyEvent event) {
//               event.consume();
//            }
//            public void keyTyped(java.awt.event.KeyEvent event) {
//               event.consume();
//            }
//         });
      }

      dialog.pack();
      dialog.setLocationRelativeTo(frame);
      dialog.setVisible(true);
   }
}

To see the problem bring up a focusable modal dialog (using ctrl-S).  Try to bring up another and you can't.  Using Alt-Tab navigate to the parent window and then try ctrl-S.  You will be able to bring up another focusable modal dialog.




======================================================================

Comments
EVALUATION I believe this is a duplicate of 5036278 (same symptoms). But I'll let AWT decide for sure. ###@###.### 2004-08-20 Should be fixed with 4255200(modal dialog should come to the front) ###@###.### ###@###.### 2004-08-23 There can be several ways to fix this bug, for example, bring the modal blocker dialog to the top or disable keyevents (including menu shortcuts) for modal blocked windows. In the first case this bug is duplicate of 4255200, in the second - duplicate of 4793073. In any case it is already fixed together with many other modality bugs. See comments to modality umbrella bug 4080029. ###@###.### 2005-06-03 11:49:43 GMT
03-06-2005

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
24-08-2004

WORK AROUND Name: rv122619 Date: 08/20/2004 Workaround demonstrated by the source code attached. ======================================================================
24-08-2004