这里的事情是在调用grabFocusForTextField()时,JTextField无法显示,因此您无法获得JTextField的焦点.为了使组件获得焦点,该组件必须首先存在,可见且可显示,启用且可聚焦.有关更多信息,请参见Focus subsystem in docs.
您必须在JFileChooser上注册自己的HierarchyListener才能侦听HierarchyEvent.在NimbusLookAndFeel中,这可能无法正确完成,或者JComboBox被选择为焦点所有者.只要组件是可显示的,只要JFileChooser的层次结构发生更改,就会触发此事件,并且此时JTextField是可显示的.
我已经重写了代码以使其正常工作.
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();
// Even if you add some other JTextField
// as accessory to JFileChooser
jf.setAccessory(new JTextField(20));
jf.addHierarchyListener(new HierarchyListener(){
public void hierarchyChanged(HierarchyEvent he)
{
grabFocusForTextField(jf.getComponents());
}
});
add(jb);
}
// Loop to find the JTextField, the first
// JTextField in JFileChooser
// Even if you setAccessory which contains a JTextField
// or which is JTextField itself, it will not get focus
private void grabFocusForTextField(Component[] c)
{
for(Component k:c)
{
if(k instanceof JTextField)
{
JTextField jt=(JTextField)k;
jt.grabFocus();
break;
}
else if(k instanceof JPanel)
{
JPanel jp=(JPanel)k;
grabFocusForTextField(jp.getComponents());
}
}
}
private void showDialog()
{
jf.showOpenDialog(this);
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new GetFocusForJTextField();
}
});
}
}
您也可以使用requestFocusInWindow()代替grabFocus()