title: How to set a Maximum and Minimum value that can be entered in a Jformattedtextfield , using DocumentFilter?
body: <p>I am newbie to Java programming. I have a document filter that only allow numeric values and decimals with "." in a <code>JFormattedTextField</code>. Now I want to implement a method within that filter, to only allow a maximum and minimum value , like I want whatever the value of number entered, it must be between [0-1], otherwise it should not accept whatever is typed.</p>
  
  <p>Now I don't want to use <code>JSpinner</code>, because from 0 to 1 there is millions of Decimals eg: 0.0001, 0.0012 ...</p>
  
  <p>This is my <code>documentFilter</code> code:</p>
  
  <pre><code>package javagui.views;
  import java.text.NumberFormat;
  import java.text.ParseException;
  import java.util.Locale;
  
  import javax.swing.text.AttributeSet;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.DocumentFilter;
  import javax.swing.text.PlainDocument;
  
  public class SeulementNumerique extends PlainDocument  {
      public static final String FLOAT = "0123456789.";
        protected String acceptedChars = null;
        protected boolean negativeAccepted = false;
        public SeulementNumerique() {
          this(FLOAT);
        }
        public SeulementNumerique(String acceptedchars) {  
          acceptedChars = acceptedchars;
        }
  
        public void setNegativeAccepted(boolean negativeaccepted) {
          if (acceptedChars.equals(FLOAT)) {
            negativeAccepted = negativeaccepted;
            acceptedChars += "-";
          }
        }
  
        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
  
          if (str == null)
            return;
          for (int i = 0; i &lt; str.length(); i++) {
            if (acceptedChars.indexOf(str.valueOf(str.charAt(i))) == -1)
              return;
          }
          if (acceptedChars.equals(FLOAT) || (acceptedChars.equals(FLOAT + "-") &amp;&amp; negativeAccepted)) {
            if (str.indexOf(".") != -1) {
              if (getText(0, getLength()).indexOf(".") != -1) {
                return;
              }
            }
          }
          if (negativeAccepted &amp;&amp; str.indexOf("-") != -1) {
            if (str.indexOf("-") != 0 || offset != 0) {
              return;
            }
          }
          super.insertString(offset, str, attr); 
        }
      }
  </code></pre>
  
  <p>Now to call for that Filter in my main code I use:</p>
  
  <pre><code>formattedTextField_5 = new JFormattedTextField();
  formattedTextField_5.setDocument(new SeulementNumerique());
  </code></pre>
  
  <p>Is there any simple way to set Maximum and Minimum value that can be entered in a JFormattedTextField?</p>