title: JTextField, using Document Filter to filter integers and periods
body: <p>EDIT - added at then end of the post the answer we were able to achieve</p>
  
  <p>This is my first post in SO, so i hope i can ask everything right!</p>
  
  <p>I searched and didn't quite find a answer to my question despite similar questions being posted, so i hope this isn't a repost.</p>
  
  <p>This is what a i got, a small application that uses <code>JTextField</code> to receive user's input and on top of that i have a <code>DocumentFilter</code> so the user can only input integers and a period in order to receive values that represent weight.</p>
  
  <p>My problem is, with my <code>DocumentFilter</code> I'm not being able to filter "copy pasted" text and i can't filter a selected text removal.</p>
  
  <p>Here is the code for the Filter</p>
  
  <pre><code>import javax.swing.text.AttributeSet;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.DocumentFilter;
  
  /**
  * The Class IntAndDotFilter.
  */
  public class IntAndDotFilter extends DocumentFilter {
  
  /** The period counter. */
  private int periodCounter = 0;
  
  /** The number counter. */
  private int numberCounter = 0;
  
  private boolean canRemove = true;
  
  public void setCanRemove(boolean canRemove) {
      this.canRemove = canRemove;
  }
  
  @Override
  public void replace(FilterBypass fb, int offset, int length, String text,
          AttributeSet attrs) throws BadLocationException {
  
      if (periodCounter == 0) { // If there is no . on the text
          if (text.matches("\\.")) { // Checks if the input is a dot
              super.replace(fb, offset, length,
                      text.replaceAll("[^0-9.]", ""), attrs);
              periodCounter++; // If it is, inserts it and saves that info
          } else {
              super.replace(fb, offset, length,
                      text.replaceAll("[^0-9]", ""), attrs);
              // If not, checks if the input is a digit
              // and inserts if it is
          }
      } else { // If there is already a .
          if (text.matches("\\.")) { // Checks if the input is another .
              super.replace(fb, offset, length,
                      text.replaceAll("[^0-9]", ""), attrs);
              // If it is, filters so that cannot be more than one .
          } else {
              if (text.matches("[0-9]")) { // Checks if it's a digit
                  if (numberCounter != 2) {
                      super.replace(fb, offset, length,
                              text.replaceAll("[^0-9]", ""), attrs);
                      numberCounter++;
                      // If yes, and if that is only the second one (0.00)
                      // inserts and
                      // saves the info that there are digits after the 1st .
                      // for removal purposes
                  } else {
                      super.replace(fb, offset, length,
                              text.replaceAll(".", ""), attrs);
                      // if it is the third+ digit after . , doesn't allow the
                      // input
                  }
              } else {
                  super.replace(fb, offset, length, text.replaceAll(".", ""),
                          attrs);
                  // Not being a digit, doesn't allow the
                  // insertion of the given input
              }
          }
      }
  }
  
  @Override
  public void remove(FilterBypass fb, int offset, int length)
          throws BadLocationException {
  
      if (canRemove) {
          if (periodCounter == 1) { // If there is a . in the text
              if (numberCounter != 0) { // and If there are digits after the .
                  numberCounter--; // It means you'r removing a digit, so it
                                      // saves
                                      // that info
                  super.remove(fb, offset, length); // And removes it
              } else { // If there are no digits it means you'r removing a .
                  periodCounter--; // It saves that info allowing a new . to
                                      // be
                                      // inserted
                  super.remove(fb, offset, length); // and removes it
              }
          } else { // If there is no . in the text there are no problems
              super.remove(fb, offset, length); // so it just removes whatever
                                                  // there is (digit)
          }
      } else {
  
      }
  }
  }
  </code></pre>
  
  <p>the insertString method does the same has the replace method so i left it out, but in the application it's implemented.</p>
  
  <p>Thanks in advance for your time!</p>
  
  <p>EDIT - Plus it now has a filter to restrain the height input too</p>
  
  <pre><code>public class IntAndDotFilter extends DocumentFilter {
  
  /** The Constant _maxCharacters. */
  private static final int _maxCharacters = 10;
  
  /** The _is weight. */
  private Boolean _isWeight = null;
  
  
  public IntAndDotFilter(Boolean isWeight) {
      super();
      _isWeight = isWeight;
  }
  
  public void replace(FilterBypass fb, int offset, int length, String string,
          AttributeSet attr) throws BadLocationException {
  
      String text = fb.getDocument().getText(0, fb.getDocument().getLength());
      text += string;
  
      if (_isWeight) {
          if ((fb.getDocument().getLength() + string.length() - length) &lt;= _maxCharacters
                  &amp;&amp; text.matches("^[1]?[0-9]{1,2}([.][0-9]{0,2})?$")) {
              super.replace(fb, offset, length, string, attr);
          } else {
              Toolkit.getDefaultToolkit().beep();
          }
      } else {
          if ((fb.getDocument().getLength() + string.length() - length) &lt;= _maxCharacters
                  &amp;&amp; text.matches("^([1]([.][0-9]{0,2})?)|([2]([.][0-5]?)?)$")) {
              super.replace(fb, offset, length, string, attr);
          } else {
              Toolkit.getDefaultToolkit().beep();
          }
      }
  }
  
  @Override
  public void remove(FilterBypass fb, int offset, int length)
          throws BadLocationException {
  
      String text = fb.getDocument().getText(0, fb.getDocument().getLength());
  
      if (_isWeight) {
          if (text.matches("^[1]?[0-9]{1,2}([.][0-9]{0,2})?$")) {
              super.remove(fb, offset, length);
          } else {
              Toolkit.getDefaultToolkit().beep();
          }
      } else {
          if (text.matches("^([1]([.][0-9]{0,2})?)|([2]([.][0-5]?)?)$")) {
              super.remove(fb, offset, length);
          } else {
              Toolkit.getDefaultToolkit().beep();
          }
      }
  }
  </code></pre>