title: documentFilter IntFilter not filtering first two characters on first run
body: <p>I have an IntFilter that will filter the characters inputted to be only integer, on the every first run of the program it will not be able to filter 1 or 2 non-integer characters but after that it will only allow integer inputs. I found this code online and combined it with a size filter, even before combining the two it was still unable to filter the first inputs only after a time will the text input be filtered.</p>
  
  <p>Can anyone help me isolate the problem here why is it that it cannot filter the first inputted characters.</p>
  
  <p>Here is the event function:</p>
  
  <pre><code>private void amountKeyReleased(java.awt.event.KeyEvent evt) {                                   
          ((AbstractDocument) amount.getDocument()).setDocumentFilter(new IntFilter(5));
      }  
  </code></pre>
  
  <p>The documentFilter:</p>
  
  <pre><code>public class IntFilter extends DocumentFilter {
  
          private int maxCharacters;    
  
          public IntFilter(int maxChars) {
              maxCharacters = maxChars;
          }
  
          @Override
          public void insertString(DocumentFilter.FilterBypass fb, int offset,
                                      String string, AttributeSet attr)
                     throws BadLocationException {
              char ch;     
              StringBuilder buffer = new StringBuilder(string);
              for (int i = buffer.length() - 1; i &gt;= 0; i--) {
                  ch = buffer.charAt(i);
                  if (!Character.isDigit(ch)) {
                      buffer.deleteCharAt(i);
                  }
              }
              if ((fb.getDocument().getLength() + string.length()) &lt;= maxCharacters){
                   super.insertString(fb, offset, buffer.toString(), attr);
              }
          }
  
          @Override
         public void replace(DocumentFilter.FilterBypass fb,
              int offset, int length, String string, AttributeSet attr) throws BadLocationException {
              if (length &gt; 0){
                 fb.remove(offset, length);
              }
              insertString(fb, offset, string, attr);
         }
      }
  </code></pre>