title: JTextField, set caret position to end of input without highlighting whole string
body: <p>I'm really baffled because I thought I solved this problem. I have a custom JDialog where the user inputs an equation, and there are buttons to add special characters to the input at the location of the cursor. Here's the function I have to add a special character: </p>
  
  <pre><code>private void addSymbol(String symbol) {
  
      int pos = input.getCaretPosition();
      String currInput = input.getText();
      input.setText(currInput.subSequence(0, pos) + symbol
              + currInput.subSequence(pos, currInput.length()));
      input.requestFocus();
      input.setCaretPosition(pos+1);
  
  }
  </code></pre>
  
  <p>I'm working in Netbeans, and yesterday this function was working as expected. You press a button, the character is added at the cursor position, and the cursor is moved to right after the new character, allowing you to continue typing without interruption, even if the added character is at the end of the string. Today, I was trying to copy my code into a different type of project, so I renamed the working project a couple of times before giving up and putting the old name back. </p>
  
  <p>Now when I try to add a special character at the end of the input, the entire input string is highlighted, so it's really easy to type something, add a character, then keep typing and accidentally overwrite everything you just input. It works fine when you add a character in the middle of the string. I tried doing a clean build, restarting Netbeans, and I removed various pieces of this function to make sure the version I was seeing was actually the one being called (I had an incident with this not being the case before, so now I'm paranoid). Does anyone know what could be going on or how to set the cursor at the end of the string without highlighting it?</p>
  
  <p>EDIT: Here's test code</p>
  
  <p>GetExpressionDialog.java</p>
  
  <pre><code>package bepe;
  
  public class GetExpressionDialog extends javax.swing.JDialog {
  
      /** Creates new form GetExpressionDialog */
      public GetExpressionDialog(java.awt.Frame parent, boolean modal) {
          super(parent, modal);
          initComponents();
      }
  
      /** This method is called from within the constructor to
       * initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is
       * always regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          input = new javax.swing.JTextField();
          andButton = new javax.swing.JButton();
          orButton = new javax.swing.JButton();
          stileButton = new javax.swing.JButton();
          messageLabel = new javax.swing.JLabel();
          submitButton = new javax.swing.JButton();
          notButton = new javax.swing.JButton();
          impliesButton = new javax.swing.JButton();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
          addWindowListener(new java.awt.event.WindowAdapter() {
              public void windowClosing(java.awt.event.WindowEvent evt) {
                  formWindowClosing(evt);
              }
          });
  
          andButton.setText("∧");
          andButton.setToolTipText("You can also type \"&amp;\"");
          andButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  andButtonActionPerformed(evt);
              }
          });
  
          orButton.setText("∨");
          orButton.setToolTipText("You can also type \"|\"");
          orButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  orButtonActionPerformed(evt);
              }
          });
  
          stileButton.setText("⊢");
          stileButton.setToolTipText("You can also type \"|-\"");
          stileButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
          stileButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  stileButtonActionPerformed(evt);
              }
          });
  
          messageLabel.setText("Enter the sequent you would like to prove:");
  
          submitButton.setText("Submit");
          submitButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  submitButtonActionPerformed(evt);
              }
          });
  
          notButton.setText("¬");
          notButton.setToolTipText("You can also type \"!\"");
          notButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  notButtonActionPerformed(evt);
              }
          });
  
          impliesButton.setText("→");
          impliesButton.setToolTipText("You can also type \"-&gt;\"");
          impliesButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  impliesButtonActionPerformed(evt);
              }
          });
  
          org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(layout.createSequentialGroup()
                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                      .add(layout.createSequentialGroup()
                          .add(20, 20, 20)
                          .add(messageLabel))
                      .add(layout.createSequentialGroup()
                          .add(20, 20, 20)
                          .add(input, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 482, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                      .add(layout.createSequentialGroup()
                          .addContainerGap()
                          .add(stileButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                          .add(notButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                          .add(andButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                          .add(orButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                          .add(impliesButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                          .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 200, Short.MAX_VALUE)
                          .add(submitButton)))
                  .addContainerGap())
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
              .add(layout.createSequentialGroup()
                  .addContainerGap()
                  .add(messageLabel)
                  .add(8, 8, 8)
                  .add(input, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                  .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                  .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                      .add(stileButton)
                      .add(notButton)
                      .add(andButton)
                      .add(orButton)
                      .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                          .add(impliesButton)
                          .add(submitButton)))
                  .addContainerGap())
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      private void stileButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  
          addSymbol("⊢");
  
      }                                           
  
      private void andButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  
          addSymbol("∧");
  
      }                                         
  
      private void orButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
  
          addSymbol("∨");
  
      }                                        
  
      private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
  
          dispose();
  
      }                                            
  
      private void notButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  
          addSymbol("¬");
  
      }                                         
  
      private void impliesButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
  
          addSymbol("→");
  
      }                                             
  
      private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
  
          input.setText("");
  
      }                                  
  
      private void addSymbol(String symbol) {
  
          int pos = input.getCaretPosition();
          String currInput = input.getText();
          input.setText(currInput.subSequence(0, pos) + symbol
                  + currInput.subSequence(pos, currInput.length()));
          input.requestFocus();
          input.setCaretPosition(pos+1);
  
      }
  
      public String getText() {
          return input.getText();
      }
  
      public javax.swing.JLabel getMessage() {
          return messageLabel;
      }
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(GetExpressionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(GetExpressionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(GetExpressionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(GetExpressionDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //&lt;/editor-fold&gt;
  
          /* Create and display the dialog */
          java.awt.EventQueue.invokeLater(new Runnable() {
  
              public void run() {
                  GetExpressionDialog dialog = new GetExpressionDialog(new javax.swing.JFrame(), true);
                  dialog.addWindowListener(new java.awt.event.WindowAdapter() {
  
                      @Override
                      public void windowClosing(java.awt.event.WindowEvent e) {
                          System.exit(0);
                      }
                  });
                  dialog.setVisible(true);
              }
          });
      }
      // Variables declaration - do not modify                     
      private javax.swing.JButton andButton;
      private javax.swing.JButton impliesButton;
      private javax.swing.JTextField input;
      private javax.swing.JLabel messageLabel;
      private javax.swing.JButton notButton;
      private javax.swing.JButton orButton;
      private javax.swing.JButton stileButton;
      private javax.swing.JButton submitButton;
      // End of variables declaration
  </code></pre>
  
  <p>test.java</p>
  
  <pre><code>package bepe;
  
  public class test {
  
      public test(){}
  
      public static void main(String[] args) {
  
          GetExpressionDialog dialog = new GetExpressionDialog(null, true);
          dialog.setVisible(true);
  
          String input = dialog.getText();
          if (input.isEmpty()) return;
  
      }
  }
  </code></pre>