KeyboardFocusManager not work well in WindowsXP/2000
I have a JButton in a frame. If clicking the button a dialog will show up. I set CurrentKeyboardFocusManager in the dialog's constructor. The part of code is:
JButton button = new JButton();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
CreateDialog dlg = new CreateDialog(frame, model);
}
});
public CreateDialog(Frame parent, MediaPresentationModel model)
{
super(parent, "Item Editor", true);
this.model = model;
this.mode = mode;
this.parent = parent;
// register a focus manager that transfers focus on 'Enter'
KeyboardFocusManager.setCurrentKeyboardFocusManager(
new DefaultKeyboardFocusManager()
{
public boolean postProcessKeyEvent(KeyEvent e)
{
Component c = e.getComponent();
// the instanceof JTable check is because we want Enter to take the
// user to the next cell not the next component...
if (e.getKeyCode() == KeyEvent.VK_ENTER &&
e.getID() == KeyEvent.KEY_PRESSED &&
!(c instanceof JTable))
{ focusNextComponent(); }
return true;
}
});
}
. . . . . .
}
There's no any problem when I run it in Linux. But in Windows only when the first time to click the button, ENTER key works fine(when clicking ENTER key,the focus went to next component); when I closed the dialog and click button to open it again, clicking ENTER key didn't do anything; the focus didn't move. I found that the first time to initiate the dialog I got the returned FocusTraversalPolicy is "LayoutFocusTraversalPolicy" but when I clicked button again the returned policy is "defaultFocusTraversalPolicy" and the returned next component is null. I initiated the dialog the same way each time why it works in Linux but not in Windows?

