title: Seprating the digits in the textfiled while entering the numbers (Javafx)
body: <p>I created a simple javafx program. I want to seprate the digits three by three when I entering the digits in the textfiled. I used two solution which are given in the stackoverflow links(<a href="https://stackoverflow.com/questions/31382250/how-to-format-text-of-textfield-javafx">How to format text of TextField? JavaFX</a>  , <a href="https://stackoverflow.com/questions/31039449/java-8-u40-textformatter-javafx-to-restrict-user-input-only-for-decimal-number/31043122#31043122">Java 8 U40 TextFormatter (JavaFX) to restrict user input only for decimal number</a>)</p>
  
  <p>but none them are working for me. the first solution(set textformatter) was useless for me(or maybe I couldn't work with it in a right way) but the second one was working but only accept 4 digits and the other numbers that I enterd in the textfield are the in the same style that I enterd them without comma.</p>
  
  <p>I want to seprate every three digits like this: 12,564,546,554
  if anyone know the solution please help me to overcome this problem.
  thanks.</p>
  
  <pre><code> import javafx.application.Application;
   import javafx.scene.Scene;
   import javafx.scene.control.TextField;
   import javafx.scene.control.TextFormatter;
   import javafx.scene.layout.HBox;
   import javafx.stage.Stage;
  
   import java.text.DecimalFormat;
   import java.text.ParsePosition;
  
  
   public class DelimiterExample extends Application{
  
  
  public static void main(String[] args) {
      launch(args);
  }
  
  
  @Override
  public void start(Stage primaryStage) throws Exception {
  
      TextField textField = new TextField();
      HBox hBox = new HBox();
  
      //solution one
  
      DecimalFormat format = new DecimalFormat( "#,###" );
  
      textField.setTextFormatter( new TextFormatter&lt;&gt;(c -&gt;
      {
          if ( c.getControlNewText().isEmpty() )
          {
              return c;
          }
  
          ParsePosition parsePosition = new ParsePosition( 0 );
          Object object = format.parse( c.getControlNewText(), parsePosition );
  
          if ( object == null || parsePosition.getIndex() &lt; c.getControlNewText().length() )
          {
              return null;
          }
          else
          {
  
              return c;
          }
  
      }));
  
  
      // solution two
  
      textField.textProperty().addListener((obs , oldVal , newVal)-&gt; {
  
          if (newVal.matches("\\d*")) {
              DecimalFormat formatter = new DecimalFormat("#,###");
              String newvalstr = formatter.format(Float.parseFloat(newVal));
              //System.out.println(newvalstr);
              textField.setText(newvalstr);
          }
  
  
      });
  
      hBox.getChildren().add(textField);
      Scene scene = new Scene(hBox , 100 , 100);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  }
  </code></pre>