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

Container operations corrupt focus handling

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.2
    • client-libs



      Name: skT88420 Date: 09/28/99


      Output for java -version:
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      Output for java -fullversion:
      java full version "JDK-1.2.2-W"


      I've included a demo program which can be found at the bottom
      in order to help me describe and reproduce what I perceive as
      a possible bug in your focus mechanism.

      Instructions on how to use the demo program:

      1.) Compile and run class A, found at the bottom.
          You should see two frames. One frame on the left, one on the right.

      2.) Click on the "Set focus -- creating new JTextFields"
          button in the right hand frame.

      3.) Click on the title bar of the left hand frame.
          (The JTextfield in the left hand frame should own the focus,
          indicated by the blinking caret.)

      4.) Repeat steps 2 and 3. Notice however that this time,
          the focus does not appear in the JTextField, but rather
          in the JComboBox.

      5.) Repeat steps 2 and 3 again. Notice that this time, the
          JTextField has the focus.

      6.) Repeat steps 2 and 3 again. Notice that the JComboBox
          now has the focus. (You should see the pattern here by
          now -- the focus keeps switching from the JTextField to
          the JComboBox)

      7.) Click on the "Set focus -- recycling JTextFields" button
          in the right hand frame.

      8.) Click on the title bar of the left hand frame.
          (The JTextfield in the left hand frame should have the
           focus.)

      9.) Repeat step 7 and 8. The JTextField should still own the
          focus.

      If I understand your focus mechanism correctly, the behavior
      demonstrated by following steps 2 and 3 is not what I would
      expect. The focus behavior demonstrated by following steps 7
      and 8 is what I would expect.

      As you can see from the source code, once the frame is fully
      initialized and visible, adding new components to a container
      has a very different effect on focus behavior than adding
      components to a container that have previously been removed.

      It should also be noted that although this is not in the
      demonstration code below, in other code that I've written,
      the mere act of removing a component from a container corrupts
      the focus mechanism for components still in that container in
      a way identical to that shown by following steps 2 and 3.

      If you need further explanation of this problem or need more
      sample code, I will be happy to provide you with it.


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

      public class A extends JFrame
      {
         JTextField tf = new JTextField(10);

         JPanel tfContainer = new JPanel();
                     
         public A()
         {
            super();
                        
            getContentPane().add(new JComboBox(), BorderLayout.NORTH);
            getContentPane().add(tfContainer, BorderLayout.CENTER);
            
            tfContainer.setLayout(new FlowLayout());
            
            tfContainer.add(tf);
         }
            
         void setFocusOnField(final boolean recycle)
         {
            tfContainer.remove(tf);
            
            if( ! recycle )
            {
               tf = new JTextField(10);
            }
            
            tfContainer.add(tf);
            
            tfContainer.doLayout();
            
            tf.requestFocus();
         }
               
         public static void main(String args[] )
         {
            final A a = new A();
            
            final JFrame f = new JFrame();

            final JButton btn1 = new JButton("Set focus -- creating new JTextFields");
            final JButton btn2 = new JButton("Set focus -- recycling JTextFields");
            
            final ActionListener l = new ActionListener()
            {
               public void actionPerformed(ActionEvent ev)
               {
                  a.setFocusOnField(ev.getSource() == btn2);
               }
            };
            
            btn1.addActionListener(l);
            
            btn2.addActionListener(l);
            
            f.getContentPane().setLayout(new GridLayout(2, 0, 5, 2));
            f.getContentPane().add(btn1);
            f.getContentPane().add(btn2);
            
            f.pack();
            f.setLocation(400, 0);
            f.setVisible(true);
            
            a.setSize(400, 400);
            a.setLocation(0,0);
            a.setVisible(true);
         }
      }
      (Review ID: 95810)
      ======================================================================

            hgajewsksunw Hania Gajewska (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: