默认情况下,JFileChooserinNimbusLookAndFeel不会显示JTextField用户键入文件路径的位置。JFileChooser中的焦点所有者JComboBox如图所示。

现在,JTextField当用户打开JFileChooser. 我尝试通过requestFocusInWindow()从递归逻辑中JTextField获取它。JFileChooser这是我完成的完整代码。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class GetFocusForJTextField extends JFrame
{
JButton jb;
JFileChooser jf;
public GetFocusForJTextField()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
// For NimbusLookAndFeel, JTextField is not
// the default focus owner in JFileChooser
try
{
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exception e){}
setTitle("Get Focus for JTextField");
setLayout(new FlowLayout());
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
jb=new JButton("Open JFileChooser");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
showDialog();
}
});
jf=new JFileChooser();
add(jb);
}
// Loop to find the JTextField, the first
// JTextField in JFileChooser
private void grabFocusForTextField(Component[] c)
{
for(Component k:c)
{
if(k instanceof JTextField)
{
JTextField jt=(JTextField)k;
jt.requestFocusInWindow();
break;
}
else if(k instanceof JPanel)
{
JPanel jp=(JPanel)k;
grabFocusForTextField(jp.getComponents());
}
}
}
private void showDialog()
{
jf.showOpenDialog(this);
grabFocusForTextField(jf.getComponents());
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new GetFocusForJTextField();
}
});
}
}
我仍然无法获得焦点。为什么我没有得到这个。