title: Limit character input in jtextfield or area
body: <p>I'm trying to limit the char input on multiple jtextfield. The two fields has different char limit and at the same time they cannot accept white space as their 1st char... for example the 1st field has only 5 char limit then the 2nd field has 10char limit.. I'm stuck on this problem here is the code that I am using:</p>
  
  <pre><code>import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.SwingUtilities;
  import javax.swing.text.AbstractDocument;
  import javax.swing.text.AttributeSet;
  import javax.swing.text.BadLocationException;
  import javax.swing.text.DocumentFilter;
  
   public class Restriction {
  
      public Restriction() {
          initComponents();
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new Restriction();
              }
          });
      }
  
      private void initComponents() {
          GridBagConstraints Cons = new GridBagConstraints();
          JFrame frame = new JFrame();
          JPanel panel = new JPanel(new GridBagLayout());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          JTextField jtf = new JTextField(20);
          JTextField jtf2 = new JTextField(20);
          //add filter to document
          ((AbstractDocument) jtf.getDocument()).setDocumentFilter(new MyDocumentFilter());
          MyDocumentFilter.charLimit(5);
          ((AbstractDocument) jtf2.getDocument()).setDocumentFilter(new MyDocumentFilter());
          MyDocumentFilter.charLimit(10);
          Cons.gridy = 0;
          panel.add(jtf, Cons);
          Cons.gridy = 1;
          panel.add(jtf2,Cons);  
          frame.add(panel);
          frame.pack();
          frame.setVisible(true);
      }
  }
  
  class MyDocumentFilter extends DocumentFilter {
  
      static int Limit;
  
      public static void charLimit(int Limitation){
          Limit = Limitation;
      }
  
      @Override
      public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
          //we want standard behavior if we are not placing space at start of JTextField
          //or if we are placing text at start of JTextField but first character is not whitespace
          if ( i!=0 &amp;&amp; i&lt; Limit || ( i==0 &amp;&amp; !Character.isWhitespace(string.charAt(0)) ) ){
              super.replace(fb, i, i1, string, as);
          }else{
              System.out.println("no spaces allowed");
          }
      }
  
      @Override
      public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
          super.remove(fb, i, i1);
      }
  
      @Override
      public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
          super.insertString(fb, i, string, as);
  
      }
  }
  </code></pre>