title: Problems with java PlainDocument overwriting values
body: <p>Good afternoon.
  I am developing a screen where I have a <strong>JTextField</strong> which should always receive <strong>UpperCase</strong> values​​. Looking, i just created a class derived from <strong>PlainDocument</strong> treating it in <strong>insertString</strong>, and it worked.
  The problem is that whenever I edit the value of my JTextField it will over-write the string instead of going in the middle of inserting string values ​​that I'm typing. Example: I have the value "<strong>João da Silva</strong>" in my field, and I want to add the name "<strong>Pedro</strong>" soon after "<strong>João</strong>" and before the "<strong>da Silva</strong>", but when I place the cursor at the position of the keyboard and I want to start enter it on-write string getting "<strong>João Pedrolva</strong>" instead of "<strong>João Pedro da Silva</strong>". I hope you have given to understand.
  Anyone know how to fix this? I searched a lot and tried many ways but could not solve.</p>
  
  <pre><code>import javax.swing.JTextField;
  import javax.swing.text.AttributeSet;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.Document;
  import javax.swing.text.PlainDocument;
  
  public class UpperCaseField extends JTextField {
  
      private static final long serialVersionUID = 1L;
  
      public UpperCaseField() {
          super();
      }
  
      protected Document createDefaultModel() {
          return new UpperCaseDocument();
      }
  
      static class UpperCaseDocument extends PlainDocument {
  
          private static final long serialVersionUID = 1L;
  
          @Override 
          public void insertString(int offs, String str, AttributeSet a) 
              throws BadLocationException {
  
              if (str == null) {
                  return;
              }
              char[] upper = str.toCharArray();
              for (int i = 0; i &lt; upper.length; i++) {
                  upper[i] = Character.toUpperCase(upper[i]);
              }
              super.insertString(offs, new String(upper), a);
          }
      }
  }
  </code></pre>
  
  <p>Thanks</p>