title: how can i force Jformattedtextfield to add commas before every 3 digits in java
body: <p>I have a problem with JFormattedTextField
  I want a JFormattedTextField so when user type a number, it separate number with a comma<br>
  example :
  user type: 1234 JFormattedTextField show 1,234<br>
  or 
  user type: 123456 JFormattedTextField show 123,456
  or
  user type: 1234567 JFormattedTextField show 1,234,567  </p>
  
  <p>I try following code in KeyTypped Event<br>
          formattedTextField = new JFormattedTextField();</p>
  
  <pre><code>    formattedTextField.setBounds(56, 159, 152, 29);
  
  
      panel_1.add(formattedTextField);
  
          formattedTextField.addKeyListener(new KeyAdapter() {
              @Override
              public void keyTyped(KeyEvent e) {
  
                    for (int i = len - 1; i &gt;= 0; i--)                      
                    {
                        result = s.charAt(i) + result;                 
                        nDigits++;                                          
                        if (((nDigits % 3) == 0) &amp;&amp; (i &gt; 0))                
                        {
                            result = "," + result;
                        }
                    }
  
                    formattedTextField.setText(result);
  
  });
  </code></pre>
  
  <p>everything is fine but two problem still exist
  1- for example, i press 5 jformattedtextfield show me 55
  i press 4 it show 44
  2- when i press backspace, jformattedtextfield doesn`t delete a number and instead of this,  type a small square
  for example :
  i press 1  formattedtextfield show me 11
  i press backspace  formattedtextfield show me 11 with a blank square</p>
  
  <p>please help me . </p>
  
  <p>thank you all in advance</p>