I tried setting the caretPosition to zero but it didn't work.
Try wrapping the code in a SwingUtilities.invokeLater()
.
If you need more help then post a MCTRE that demonstrates the problem.
Your code is overly complicated. You only need to reset the caret for the text field that generated the event (not all of the text fields):
class TextFieldFocusListener implements FocusListener
{
public void focusGained(FocusEvent etffl)
{
JTextField textField = (JTextField)etffl.getComponent();
textField.setCaretPosition(textField.getDocument().getLength());
}
public void focusLost(FocusEvent etffl)
{
JTextField textField = (JTextField)etffl.getComponent();
textField.setCaretPosition(0);
}
}