Andrew,
I always start with the docs. I don't need pointer to docs..
I apparently require a crystal ball...
I have tried requestFocus and requestFocusInWindow and no change.
...to know that you tried calling both Component.requestFocus()
and Component.requestFocus(true).
OK.. here is the quick example I whipped up to check my facts.
<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFocus
extends JPanel implements ActionListener {
JTextField[] tf;
public TestFocus() {
tf = new JTextField[3];
tf[0] = new JTextField("Blah!");
tf[1] = new JTextField();
tf[2]= new JTextField("Tah!");
setLayout( new GridLayout() );
add(tf[0]);
add(tf[1]);
add(tf[2]);
JButton b = new JButton("Focus");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent ae) {
for (int ii=0; ii<tf.length; ii++) {
String text = tf[ii].getText().trim();
if (text.equals("")) {
JOptionPane.showMessageDialog(tf[ii],
"Please enter details", "Data Error",
JOptionPane.WARNING_MESSAGE );
// need set focus here
tf[ii].requestFocus();
return;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Test Focus");
frame.setDefaultCloseOperation(
JFrame.DISPOSE_ON_CLOSE );
frame.getContentPane().add( new TestFocus() );
frame.pack();
frame.setVisible(true);
}
}
</sscce>
This code works to set the focus to the second text field for me.
Does it work for you? Yes?
In that case, the answer is to change that code, one line at a time,
to your code. Then, when it breaks, that is the bit you are doing wrong.
If that fails to produce your solution, try making an SSCCE*, as I did.
* <
http://www.physci.org/codes/sscce.jsp>
HTH