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

Focus cycle root does not take initial focus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs
    • x86
    • windows_2000

      Name: gm110360 Date: 02/10/2003


      FULL PRODUCT VERSION :
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]
      Service Pack 2

      A DESCRIPTION OF THE PROBLEM :
      If the object being used as the content pane of a JFrame
      (via. JFrame.setContentPane) is set as a focus cycle root
      and has a FocusTraversalPolicy set, the initial focus when
      the frame is made visible is not being set correctly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.(see attached source code)
      2. Run Frame1 and you will see that focus is placed in the
      text field once the frame opens.
      3. Run Frame2 and you will see that it is not...focus is
      actually given to the frame itself.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      It would make sense that if a focus cycle root is found
      while trying to find the initial component to give focus to
      that it should get the FocusTraversalPolicy from that cycle
      root and call FocusTraversalPolicy.getInitialComponent on
      that policy. You can tell from the lack of print outs that
      it is not, in fact, attempting to do this.

      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /**
       * This is the first class...it works as expected
       */
      public class Frame1
      {
          public static void main(String [] args)
          {
              JFrame f = new JFrame();
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              JPanel p = new JPanel(new BorderLayout());
              p.add(new JTextField(12), "Center");
              p.add(new JButton("Press Me"), "South");
              f.setContentPane(p);

              f.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy(){
                  public Component getInitialComponent(Window w)
                  {
                      Component res = super.getInitialComponent(w);
                      System.out.println("initial comp: " + res);
                      return res;
                  }
              });

              f.pack();
              f.setVisible(true);
          }
      }

      /**
       * This is the second class....this one doesn't work as expected
       */
      public class Frame2
      {
          public static void main(String [] args)
          {
              JFrame f = new JFrame();
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              JPanel p = new JPanel(new BorderLayout());
              p.add(new JTextField(12), "Center");
              p.add(new JButton("Press Me"), "South");

              p.setFocusCycleRoot(true);
              p.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy(){
                  public Component getFirstComponent(Container c)
                  {
                      Component res = super.getFirstComponent(c);

                      System.out.println("first comp: " + res);
                      return res;
                  }
              });
              f.setContentPane(p);

              f.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy(){
                  public Component getInitialComponent(Window w)
                  {
                      Component res = super.getInitialComponent(w);
                      System.out.println("initial comp: " + res);
                      return res;
                  }
              });

              f.pack();
              f.setVisible(true);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      1) Override the FocusTraversalPolicy to add to the JFrame
      that will do the correct thing for getInitialComponent(...)

      2) Instead of setting the FocusTraversalPolicy on the
      content pane, just set it on the JFrame itself.
      (Review ID: 146534)
      ======================================================================

            ant Anton Tarasov (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: