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

JDialog has no keyboard input focus in JRE 1.6 when launched from Applet

XMLWordPrintable

    • 6
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Executed using Microsoft Internet Explorer 7.0 with all latest security fixes.

      A DESCRIPTION OF THE PROBLEM :
      Applet in a web page that opens up a modal JDialog with 2 buttons.

      In JRE 1.5.0_05 this worked great but using exactly the same code with JRE 1.6, the JDialog does not always get the keyboard input focus after I call setVisible(true). This seems to happen 9 out of 10 times (i.e. it rarely gets the focus).

      Only when I click in the dialog do the focus event methods get fired.

      I've made all the fields in the dialog focusable and I've tried calling requestFocus() before I show the dialog (tried this on EventQueue.InvokeLater() too) all to no avail.

      Running the same code using the Applet viewer does not show the problem only when using the plug-in through IE.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the Applet and dialog code below into a JAR file called test.jar and create a HTML file, using the supplied code below, in the same directory as jar and open the file using IE 7.0 and the 1.6 JRE on Windows XP. Click the "Open JDialog" and you should see the dialog does not have focus. Run using the 1.5 JRE and it does.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When the "Open JDialog" button is pressed it opens a popup that has the keyboard focus. Pressing the space bar will activate the focussed button or pressing tab will move the focus between the buttons.


      ACTUAL -
      When the "Open JDialog" button is pressed it opens a popup that does not have the keyboard focus. Pressing tab or space does nothing. Only by clicking down in the button (notice the focus trace appears in the console) with the mouse and then releasing the button outside the dialog do you get the focus.

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      Applet Code:

      package com.test;
       
      import java.applet.Applet;
      import java.awt.Frame;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
       
      import javax.swing.JButton;
       
      public class BasicApplet extends Applet implements ActionListener
      {
      private static final long serialVersionUID = 2357689359732807325;
      private JButton open = null;

      public void init()
      {
      super.init();

      open = new JButton("Open JDialog");
      open.addActionListener(this);
      open.setLocation(10, 10);
      add(open);
      }
       
      public void actionPerformed(ActionEvent e)
      {
      if (e.getSource() instanceof JButton)
      {
                  Frame frame = new Frame();
                  BasicDialog dc = new BasicDialog(frame);
                  dc.setLocation(500, 200);
                  dc.setSize(200, 200);
                  dc.select();
      }
      }
      }

      Dialog Code:

      package com.test;
       
      import java.awt.Component;
      import java.awt.Dialog;
      import java.awt.Frame;
      import java.awt.SystemColor;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.event.FocusEvent;
      import java.awt.event.FocusListener;
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseListener;
       
      import javax.swing.BorderFactory;
      import javax.swing.JButton;
      import javax.swing.JDialog;
      import javax.swing.JPanel;
      import javax.swing.SwingUtilities;
       
      public class BasicDialog extends JDialog
      implements MouseListener, FocusListener,
      KeyListener, ActionListener
      {
      private static final long serialVersionUID = 8018620191929621139;

      private JButton ok; // "Ok" button.
          private JButton cancel; // "Cancel" button.
          public boolean focusLost = false; // focus change caused close
          public boolean okClicked = false;
          
          private void construct()
          {
              // Turn off window border
              setUndecorated(true);
              addFocusListener(this);
              
              // Create content pane
              JPanel content = new JPanel();
              content.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaptionBorder,1));
              content.addFocusListener(this);
              
              
              // Create the OK button
              //
              ok = new JButton("OK");
              ok.addActionListener(this);
              ok.setFocusable(true);
              ok.addKeyListener(this);
              ok.addFocusListener(this);
              ok.setOpaque(false);
              
              // Create the cancel button
              //
              cancel = new JButton("Cancel");
              cancel.addActionListener(this);
              cancel.setFocusable(true);
              cancel.addKeyListener(this);
              cancel.addFocusListener(this);
              cancel.setOpaque(false);
              
              // Create a buttons panel
              //
              JPanel buttons = new JPanel();
              buttons.add(ok);
              buttons.add(cancel);
              buttons.setBackground(SystemColor.inactiveCaption);
              buttons.addFocusListener(this);
              
              // Add all the controls to the content panel
              //
              content.setLayout(getContentPane().getLayout());
              content.add("South", buttons);
              content.doLayout();
              
              // Update the dialog
              //
              setContentPane(content);
              pack();
              setResizable(false);
              
              buttons.requestFocus();
          }
       
          /**
           * Called when the "Ok" button is pressed. Just sets a flag and hides the
           * dialog box.
           */
          public void actionPerformed(ActionEvent e)
          {
              if (e.getSource() == ok) okClicked = true;
              setVisible(false);
          }
       
          public void focusGained(FocusEvent e)
          {
           System.out.println("****** Date got focus " + e.getSource());
          }
       
          public void focusLost(FocusEvent e)
          {
           System.out.println("****** Date lost focus" + e.getSource());
          }
       
          public void keyPressed(KeyEvent e)
          {
           System.out.println("Key pressed " + e);
          
              // Deal with panel specific keys
              //
              switch (e.getKeyCode())
              {
               case KeyEvent.VK_ESCAPE:
               setVisible(false);
               break;
       
               case KeyEvent.VK_TAB:
               Component comp = getFocusOwner();
               if (e.isShiftDown())
               comp.transferFocusBackward();
               else
               comp.transferFocus();
               break;
              
               case KeyEvent.VK_ENTER:
                      if (e.getSource() != cancel) okClicked = true;
                      setVisible(false);
               break;
              }
          }
       
          public void mouseClicked(MouseEvent e)
          {
           System.out.println("Mouse clicked" + e);
          }
       
          public void keyReleased(KeyEvent e)
          {
          }
       
          public void keyTyped(KeyEvent e)
          {
          }
       
          public void mouseEntered(MouseEvent e)
          {
          }
       
          public void mouseExited(MouseEvent e)
          {
          }
       
          public void mousePressed(MouseEvent e)
          {
          }
       
          public void mouseReleased(MouseEvent e)
          {
          }
       
          public BasicDialog(Dialog owner, String title)
          {
              super(owner, title, true);
              construct();
          }
       
          public BasicDialog(Dialog owner)
          {
              super(owner, true);
              construct();
          }
       
          public BasicDialog(Frame owner, String title)
          {
              super(owner, title, true);
              construct();
          }
       
          public BasicDialog(Frame owner)
          {
              super(owner, true);
              construct();
          }
       
          public void select()
          {
           System.out.println("Select >>>");
       
           this.addWindowListener(new java.awt.event.WindowAdapter()
              {
               public void windowOpened(java.awt.event.WindowEvent e)
               {
               System.out.println("Window opened");
               SwingUtilities.invokeLater(new Runnable()
               {
               public void run()
               {
                       System.out.println("requestFocusInWindow");
               ok.requestFocusInWindow();
               }
               });
               }
              });
              
              okClicked = false;
           System.out.println("Before set visible");
              setVisible(true);
           System.out.println("After set visible");
              
              if (!okClicked) return;
              
           System.out.println("Select <<<");
              return;
          }
      }

      I compiled these into a JAR file (test.jar) and loaded them into some basic HTML that shows the problem (no keyboard focus).

      HTML

      <HTML>
      <BODY BGCOLOR="#FFFFFF">
        <CENTER>
       
          <TABLE BORDER="3" CELLSPACING="0" CELLPADDING="5">
      <TR>
      <TD ALIGN="CENTER" BGCOLOR="#C0C0C0">
       
      <APPLET CODEBASE="classes" ARCHIVE="test.jar"
      CODE="com/test/BasicApplet.class"
      WIDTH=600
      HEIGHT=130>
      </APPLET>
      </TD>
      </TR>
          </TABLE>
        </CENTER>
      </BODY>
      </HTML>


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

      CUSTOMER SUBMITTED WORKAROUND :
      None that I can find. If there is one I'll use it.

      Release Regression From : 5.0u12
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

            herrick Andy Herrick (Inactive)
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: