title: How do I get the cursor to the TOP of a JTextArea inside a JScrollPane
body: <p>For a long time I've been living with the cursor being at the bottom of my <code>JTextArea</code> named <code>txaOutput</code> after being populated in a <code>JScrollPane</code> even though I WANT it positioned at the TOP.</p>
  
  <p>Tthe only help I've found has referred to setting the caret position to 0 (e.g., <code>txaOutput.setCaretPosition(0);</code>), which I'd never figured out how to make work properly.</p>
  
  <p>Today I just went through every conceivable method for <code>JTextArea</code> and finally found that, BEFORE populating it, this line seems to do what I need:</p>
  
  <p><code>txaOutput.insert(" ", 1 );</code></p>
  
  <p>SURELY this isn't the best or only way.</p>
  
  <p>Here's the class for the text area:</p>
  
  <pre><code>package masterwords;
  import gbl.GBConstraints;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.GridBagLayout;
  import javax.swing.BorderFactory;
  import javax.swing.JFrame;
  import javax.swing.JScrollPane;
  import javax.swing.JTextArea;
  import javax.swing.ScrollPaneConstants;
  import javax.swing.WindowConstants;
  
  public class HelpOutput extends JFrame {
  
    private JScrollPane scrPnl;
    private JTextArea txaOutput;  
  
    public     HelpOutput() /* constructor */ {
  
      scrPnl = new JScrollPane();
  
      txaOutput    = new JTextArea();
  
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  
      scrPnl.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
      scrPnl.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
      scrPnl.setViewportBorder(BorderFactory.createEtchedBorder());
      scrPnl.setAutoscrolls(false);
      scrPnl.setPreferredSize(new Dimension(500, 555));
  
      txaOutput.setFont(new Font("Courier New", 0, 14)); 
      scrPnl.setViewportView(txaOutput);
  
      setLayout(new GridBagLayout());
  
      add(scrPnl, new GBConstraints(0,0).ipad(200, 300).spanX(100).spanY(90));
  
      txaOutput.insert(" ", 1 );  // ********** WITHOUT THIS CURSOR IS AT BOTTOM
      setVisible(true);
      pack();
    }
  
    public void appendHelp(String s){
      txaOutput.append(s);
    }
  }
  </code></pre>
  
  <p>Here's how I've always called it but it NEVER worked until adding line with ************ above:</p>
  
  <pre><code>  private void btnHelpActionPerformed (ActionEvent evt) {                                        
  
      HelpOutput helpOutput = new HelpOutput();
  
      Scanner sc = openHelp();
      while(sc.hasNext())
        helpOutput.appendHelp(sc.next());
  
     // txaOutput.setCaretPosition(0); // THIS DOES NOTHING so commented out!!!
    }   
  </code></pre>
  
  <p>Prior to adding the line with all the **************s, NOTHING ELSE I tried put the cursor at the TOP of the text area--always the bottom.</p>
  
  <p>WHAT SHOULD I BE DOING? What I AM doing seems a kludge.</p>
  
  <p><strong>* EDIT, THANKS TO JAVANATOR *</strong></p>
  
  <p>Rename variable <code>txaOutput</code> to <code>txaHelpOutput</code>; problem solved. New key lines:</p>
  
  <pre><code>  private void btnHelpActionPerformed (ActionEvent evt) {                                        
  
      HelpOutput helpOutput = new HelpOutput();
  
      Scanner sc = openHelp();
      while(sc.hasNext())
        helpOutput.appendHelp(sc.next());
  
     txaHelpOutput.setCaretPosition(0);
     // ^^^^
    }   
  
  
  public class HelpOutput extends JFrame {
  
    private JTextArea txaHelpOutput;  
    //                   ^^^^
  
    public     HelpOutput() /* constructor */ {
  
      txaHelpOutput    = new JTextArea();
      // ^^^^
  
      scrPnl.setViewportView(txaHelpOutput);
      //                        ^^^^
  
      // LOSE THIS LINE!! txaHelpOutput.insert(" ", 1 ); 
    }
  </code></pre>