JDK-4276218 : Container operations corrupt focus handling
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-09-28
  • Updated: 2000-10-26
  • Resolved: 2000-10-26
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
EVALUATION This bug appears to have been there for a long time (if indeed it is a bug -- I need to read the source code before concluding this. Since the source code is adding fields, it may be completely reasonable, depending on the focus manager policy, that the focus will end up not where it used to be, but in some new place. Still, the fact that it behaves differently on Solaris and Windows is a problem). As indicated above, the code is somewhat esoteric, so this is by no means primary functionality. hania.gajewska@Eng 2000-01-14 Name: pzR10082 Date: 10/26/2000 This bug is not reproducible in Merlin after focus changes. Will be closed as duplicate of 4290675. ###@###.### 2000-10-26 ====================================================================== Name: dkR10074 Date: 11/01/2000 ###@###.### 2000-11-01 Windows_NT_x86 promoted b34 - FAILED promoted b35 - PASSED promoted b36 - PASSED I've found a difference between b35 and b36. b35 is ok. b36 has one feature. When I click twice a button in the right frame, the button loses the focus temporarily after the first click and gains it back after the second one. ======================================================================
11-06-2004