title: How to format a text field javafx
body: <p>I would like to be able to enter the time in a javafx text field in the format hh:mm:ss. How would I go about formatting the text field? I already got the regex method so it only accepts numbers as inputs:</p>
  
  <pre><code>public void format(TextField t, String regex){
      TextFormatter&lt;String&gt; formatter = new TextFormatter&lt;String&gt;( change -&gt; {
          change.setText(change.getText().replaceAll(regex, ""));
          return change; 
  
  
      });
      t.setTextFormatter(formatter);
  }
  </code></pre>
  
  <p>and I also got a method that limits the amount of characters allowed in the textfield:</p>
  
  <pre><code>public void limitLength(int maxLength, TextField t){
      t.lengthProperty().addListener(new ChangeListener&lt;Number&gt;() {
  
          @Override
          public void changed(ObservableValue&lt;? extends Number&gt; observable,
                  Number oldValue, Number newValue) {
              if (newValue.intValue() &gt; oldValue.intValue()) {
                  // Check if the new character is greater than LIMIT
                  if (t.getText().length() &gt;= maxLength) {
  
                      t.setText(t.getText().substring(0, maxLength));
                  }
              }
          }
      });
  }
  </code></pre>
  
  <p>However none of this helps me get the time format I want. The ideal would be the initial text being 00:00:00 and the user ONLY being able to edit the digits, unable to touch the colons. It would also be awesome if I could be able to make sure that between each colon there are 2 digits, ie the user should be unable to move around the digits, like this 0:000:00.</p>
  
  <p>Thanks for any help.</p>