JDK-6411406 : Components automatically transfer focus on removal, even if developer requests focus elsewhere first
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2006-04-11
  • Updated: 2011-03-07
  • Resolved: 2011-03-07
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
7 b07Fixed
Related Reports
Relates :  
Relates :  
Description
When a component is removed from the containment hierarchy, AWT automatically transfers focus elsewhere. Unfortunately, it doesn't do it gracefully like when a component is made invisible/disabled/etc...For those states, the code flows through autoTransferFocus() which first looks for other pending focus requests. This is not the case on removal. As such, even if you request focus elsewhere first, removing a component causes focus to go wherevere AWT wishes. Test case below:

Compile and run.
Put focus on the textfield with text "two"
Hit the button.

EXPECTED: focus should be on text field one, since it was requested before removing two
ACTUAL: focus is on the button due to AWT transfer

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

public class FocusTest extends JFrame {
    
    public FocusTest() {
        super("FocusTest");
        
        setLayout(new GridLayout(3, 1));
        final JTextField one = new JTextField("one");
        final JTextField two = new JTextField("two");
        
        JButton but = new JButton("Do It");
        but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                one.requestFocus();
                getContentPane().remove(two);
                ((JPanel)getContentPane()).revalidate();
                getContentPane().repaint();
            }
        });
        
        getContentPane().add(one);
        getContentPane().add(two);
        getContentPane().add(but);
        
        but.setRequestFocusEnabled(false);
    }

    private static void createAndShowGUI(String[] args) {
        FocusTest test = new FocusTest();
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setSize(400, 400);
        test.setLocationRelativeTo(null);
        test.setVisible(true);
    }
    
    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI(args);
            }
        });
    }
}

Comments
EVALUATION Auto focus transfer in general has caused numerous problems in Swing. I was hoping that this small fix would solve those problems. Unfortunately it alone does not. More work needs to be done on the Swing (and maybe AWT) side to address the Swing problems. As such, fixing this bug alone is not urgent for Swing for Mustang. I have already worked around the original problem that caused me to file this bug. I'm okay with this being targeted at Dolphin.
25-04-2006

SUGGESTED FIX --- Component.java 2006-04-20 11:19:42.000000000 +0400 *************** *** 6456,6462 **** } synchronized (getTreeLock()) { ! if (isFocusOwner() && !nextFocusHelper()) { KeyboardFocusManager.getCurrentKeyboardFocusManager(). clearGlobalFocusOwner(); } --- 6456,6465 ---- } synchronized (getTreeLock()) { ! if (isFocusOwner() ! && !KeyboardFocusManager.hasFocusRequests() ! && !nextFocusHelper()) ! { KeyboardFocusManager.getCurrentKeyboardFocusManager(). clearGlobalFocusOwner(); }
25-04-2006

EVALUATION the fix is very short/simple, but it affects Component (thus ALL components). So I need to test for some time. Also, it would be nice to understand how critical (for Swing) to have this bug fixed in mustang.
25-04-2006

EVALUATION should consider fixing this in Mustang
12-04-2006