JDK-6515061 : Incorrect focus owner when hiding the components
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.2,7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2007-01-19
  • Updated: 2011-05-18
  • Resolved: 2011-05-18
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 b17Fixed
Related Reports
Relates :  
Relates :  
Description
This issue is reproducable on all platforms starting from 1.4.2.

I've a Frame with three buttons. When the first button is pressed, I'm hiding (setVisible(false)) all the three buttons. KeyboardFocusManager.getFocusOwner method returns the second button on XToolkit even after all the buttons are hidden. On Windows, the same method returns the parent frame. On XToolkit, pressing space key after making the buttons hidden, triggers ActionEvent for the second button, even though it is hidden.

To reproduce:
1. Run the below testcase
2. Click the 'Hide buttons' button which will hide the three buttons
3. Click the non-focusable button to see the focus owner

import java.awt.*;
import java.awt.event.*;

public class FocusTest {
    
    private Button b1, b2, b3, b4;
    private Frame f;
    
    public static void main(String[] args) {
        new FocusTest();
    }
    
    public FocusTest() {
        f = new Frame();
        f.setLayout(new FlowLayout());
        b1 = new Button("Hide buttons");
        b2 = new Button("B2");
        b3 = new Button("B3");
        f.add(b1);
        f.add(b2);
        f.add(b3);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                b1.setVisible(false);
                b2.setVisible(false);
                b3.setVisible(false);
            }
        });
        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("b2 - Action Performed");
            }
        });
        b4 = new Button("Check FocusOwner");
        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("Focus Owner: " +                                KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
            }
        });
        b4.setFocusable(false);
        f.add(b4);
        f.setSize(200, 200);
        f.setVisible(true);
    }
}

This bug disappears when the order of hiding the buttons is changed. ie, if the buttons are hidden in the following order, the bug disappears:

b2.setVisible(false);
b3.setVisible(false);
b1.setVisible(false);

Incorrect value is returned by KeyboardFocusManager.getFocusOwner method even when buttons are removed from the frame.

Comments
SUGGESTED FIX see 4726458
25-06-2007

EVALUATION the problem has been fixed by the fix for 4726458.
25-06-2007

EVALUATION one more problem with auto focus transfer. perhaps we already have similar bug, but I will keep this for collection :)
19-01-2007