title: JTextField - changing caret position on focusLost event
body: <p>I have 5 <code>JTextField</code>s in a <code>JFrame</code> and I have added a <code>FocusListener</code> to all of them in a loop.</p>
  
  <p>On <code>focusGained()</code> event, I'm moving the caret to the end of the <code>JTextField</code> using <code>setCaretPosition()</code> method.</p>
  
  <p>On <code>focusLost()</code> event, I'm trying to move the caret to the beginning of the <code>JTextField</code> so that the text inside the respective field can be read from the beginning. </p>
  
  <p>I can't figure out what to do in the focusLost event. I tried setting the caretPosition to zero but it didn't work.</p>
  
  <p>Could someone help me out here?</p>
  
  <p>Edit:</p>
  
  <p>Here's the SSCCE:</p>
  
  <pre><code>public void focusGained(FocusEvent etffl)
  {
   for(int i = 0; i &lt; 5; i++)
   {
    field[i].setCaretPosition(field[i].getText().length());
   }
  }
  public void focusLost(FocusEvent etffl)
  {
   for(int i = 0; i &lt; 5; i++)
   {
    field[i].setCaretPosition(0);
   }
  }
  </code></pre>
  
  <p>Edit #2:</p>
  
  <p>Here's the MCTRE:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.event.*;
  
      class SampleGUI
  
      {
       public static JTextField[] field = new JTextField[5];
       public static void main(String[] args)
  
       {
        JFrame frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
        final JPanel panel = new JPanel();
  
        for(int i=0; i&lt;5; i++)
        {
         field[i] = new JTextField(20);
         field[i].addFocusListener(new TextFieldFocusListener());
         panel.add(field[i]);
        }
  
        frame.add(panel);
        frame.setSize(300,300);
        frame.setVisible(true);
  
       }
  
      }
  
      //TextFieldFocusListener
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  import javax.swing.event.*;
  
  class TextFieldFocusListener implements FocusListener
  
  {
  
   public void focusGained(FocusEvent etffl)
   {
    for(int i = 0; i &lt; 5; i++)
    {
     SampleGUI.field[i].setCaretPosition(SampleGUI.field[i].getText().length());
    }
   }
   public void focusLost(FocusEvent etffl)
   {
    for(int i = 0; i &lt; 5; i++)
    {
     SampleGUI.field[i].setCaretPosition(0);
    }
   }
  }
  </code></pre>