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

Component.requestFocus() doesn't work after showing JWindow the first time.

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_2000

      Description



      Name: boT120536 Date: 04/12/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      I get this problem where requestFocus() does not work for components in a
      JWindow immediately after the JWindow becomes visible. I have the problem in
      JDK 1.3 on Windows 2000. The problem does not appear in JDK 1.2.2. This looks
      very similar to bug # 4199374, which was fixed in 1.2. The following code will
      demonstrate the problem:

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      /**
       * This is a component, which can process keyboard events, shows itself in a
      JWindow.
       * It is gray when it doesn't have the focus and white when it does. Click on
      it to give
       * it the focus. Press Enter to pop up a new JWindow with a new component. The
      new
       * component should get the focus, but it never does when it is first shown; you
      have to
       * press Enter a second time. Press Escape to hide the component.
       */
      public class FocusTest extends JPanel
      {
         public FocusTest()
         {
            enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
            setBorder(BorderFactory.createLineBorder(Color.red, 3));
         }

         public Dimension getPreferredSize()
         {
            return new Dimension(100, 100);
         }

         public void processKeyEvent(KeyEvent e)
         {
            super.processKeyEvent(e);

            if (e.getID()==KeyEvent.KEY_PRESSED)
            {
               switch(e.getKeyCode())
               {
                  case KeyEvent.VK_ENTER:
                     showSubPanel();
                     break;
                  case KeyEvent.VK_ESCAPE:
                     hideWindow();
                     break;
                  default:
                     lastChar[0] = e.getKeyChar();
                     repaint();
                     break;
               }
            }
         }

         public void processMouseEvent(MouseEvent e)
         {
            super.processMouseEvent(e);

            if (e.getID()==MouseEvent.MOUSE_PRESSED)
            {
               if (subpanel!=null) subpanel.hideWindow();
               requestFocus();
            }
         }

         public void paintComponent(Graphics g)
         {
            setBackground(hasFocus()? Color.white : Color.gray);
            super.paintComponent(g);
            g.drawChars(lastChar, 0, 1, 50, 50);
         }

         void showSubPanel()
         {
            if (subpanel==null)
            {
               subpanel = new FocusTest();
            }
            subpanel.showWindow(this, 50, 50);
         }

         void showWindow(Component c, int x, int y)
         {
            if (window==null)
            {
               window = new JWindow();
               window.getContentPane().add(this);
               window.pack();
            }
            Point p = new Point(x, y);
            if (c!=null) SwingUtilities.convertPointToScreen(p, c);
            window.setLocation(p);
            // calling show() twice is a workaround
            // except for the very first component, I don't know why
            //window.show();
            window.show();

            // this call to requestFocus() doesn't work the first time
            requestFocus();
            repaint();
         }

         public void requestFocus()
         {
            System.out.println(Integer.toString(hashCode(), 16) + ':');
            System.out.println("\tI am " + (isVisible()? "" : "not ") + "visible.");
            // comment out for versions older than 1.3
            System.out.println("\tMy root window is " +
      (SwingUtilities.getWindowAncestor(this).isVisible()? "" : "not ") + "visible.");
            super.requestFocus();
            System.out.println("\tI " + (hasFocus()? "" : "do not ") + "have the
      focus.");
            repaint();
         }

         void hideWindow()
         {
            if (window!=null) window.hide();
         }
         
         private FocusTest subpanel;
         private JWindow window;
         private char[] lastChar = { ' ' };

         public static void main(String[] arg)
         {
            new FocusTest().showWindow(null, 0, 0);
         }
      }
      (Review ID: 120611)
      ======================================================================

        Attachments

          Activity

            People

            • Assignee:
              son Oleg Sukhodolsky (Inactive)
              Reporter:
              bonealsunw Bret O'neal (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: