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.