JDK-4801661 : 1.4.1 REGRESSION: Wrong Focus when JSplitPane removed+added to window
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1_01
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-01-10
  • Updated: 2007-10-30
  • Resolved: 2007-10-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7Resolved
Related Reports
Relates :  
Relates :  
Description
Name: jk109818			Date: 01/10/2003


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

FULL OPERATING SYSTEM VERSION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
If a JSplitPane containing top & bottom components is added
to a JFrame, then subsequently the JSplitPane is removed
and then readded to the JFrame when the window is focused,
the bottom component of the JSplitPane will always gain
focus shortly afterwards, irrespective of current focus or
previous focus state of the JSplitPane.

This also occurs with similarly layed out components - e.g.
a JPanel with BorderLayout and components added to NORTH &
SOUTH.

This is a regression from 1.4.0 - it is not present in
1.4.0_03.  I did not have earlier versions of 1.4.1 to test
with.  I think this may be linked to bug 4625667, but that
involved tabbed panes.

REGRESSION.  Last worked in version 1.4

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Run the attached app.
2. Click the button marked "Click Me"

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected output:

TOP gained focus.
Content Pane Rebuilt.

Actual output:

TOP gained focus.
Content Pane Rebuilt.
BOTTOM gained focus.


The last line of actual output shows the error occurring.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class FocusBugTest
{
  public static void main(String[] args)
  {
    final JFrame frame = new JFrame("Focus Problem Test");
    JTextArea textArea1 = new SelfDescribingTextArea("TOP");
    JTextArea textArea2 = new SelfDescribingTextArea("BOTTOM");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
textArea1, textArea2);
    frame.getContentPane().add(splitPane, BorderLayout.CENTER);

    JButton button = new JButton("Click Me");
    button.addActionListener(
      new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          frame.getContentPane().remove(splitPane);
          frame.getContentPane().add(splitPane, BorderLayout.CENTER);
          System.out.println("Content Pane Rebuilt.");
        }
      }
    );
    button.setFocusable(false);
    frame.getContentPane().add(button, BorderLayout.SOUTH);
    frame.setSize(400,300);

    DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager
().addPropertyChangeListener(
      "permanentFocusOwner",
      new PropertyChangeListener()
      {
        public void propertyChange(PropertyChangeEvent evt)
        {
          final Object newFocusedComponent = evt.getNewValue();
          if (newFocusedComponent != null)
            System.out.println(newFocusedComponent + " gained focus.");
        }
      }
    );
    frame.show();
  }

  private static class SelfDescribingTextArea extends JTextArea
  {
    public SelfDescribingTextArea(String text)
    {
      super(text);
    }

    public String toString()
    {
      return getText();
    }
  }
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
I have found no means of working round this issue as the
focus event for the bottom component appears to arrive both
asynchronously to and after the rebuilding of the component
hierarchy and any focus requests made during rebuilding.

Release Regression From : 1.4.0_03
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Review ID: 179216) 
======================================================================

Comments
EVALUATION The correct behavior is as follows. When the focus owner is removed and there's no component to auto transfer focus to (the test case), focus is cleared. So, after adding the panel back, no focus owner should exist. This is how it bahaves since 1.5. So, I'm closing the CR as no longer reproducible.
30-10-2007

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
22-09-2004

EVALUATION Name: apR10133 Date: 01/15/2003 This bug was introduced with the fix for bugs #4511021 and #4604625. These bugs were fixed with the following comments: "Now we do not remove focus related events from the EventQueue in removeNotify(), so that they can still be dispatched and the fields of KeyboardFocusManager and DefaultKeyboardFocusManager that are updated on response to these events are updated properly and the events that previously were lost now arrive." So, when the top button calls removeNotify() it transfer focus to the bottom button and the appropriate focus event is not removed from the EventQueue, but it sent to the bottom button which is already added back to the frame. Hence the bottom button get the focus. ###@###.### ====================================================================== Name: apR10133 Date: 01/16/2003 This problem was introduced with the changes in AWT and its cause is similar to the bugs #4511021, #4495338 and #4604625. ###@###.### ======================================================================
22-09-2004