title: Recommended way to restrict input in JavaFX textfield
body: <p>It may seem the question is the duplicate of <a href="https://stackoverflow.com/questions/7555564/what-is-the-recommended-way-to-make-a-numeric-textfield-in-javafx">this</a>. But my question is i have developed a integer textfield in JavaFX by two ways. The code is given below</p>
  
  <pre><code>public class FXNDigitsField extends TextField
  {
  private long m_digit;
  public FXNDigitsField()
  {
      super();
  }
  public FXNDigitsField(long number)
  {
      super();
      this.m_digit = number;
      onInitialization();
  }
  
  private void onInitialization()
  {
      setText(Long.toString(this.m_digit));
  }
  
  @Override
  public void replaceText(int startIndex, int endIndex, String text)
  {
      if (text.matches(Constants.DIGITS_PATTERN) || text.equals(Constants.EMPTY_STRING)) {
          super.replaceText(startIndex, endIndex, text);
      }
  }
  
  @Override
  public void replaceSelection(String text)
  {
      if (text.matches(Constants.DIGITS_PATTERN) || text.equals(Constants.EMPTY_STRING)) {
          super.replaceSelection(text);
      }
  }
  }
  </code></pre>
  
  <p>And the second way is by adding an event Filter. </p>
  
  <p>The code snippet is given.</p>
  
  <pre><code> // restrict key input to numerals.
  this.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler&lt;KeyEvent&gt;() {
    @Override public void handle(KeyEvent keyEvent) {
      if (!"0123456789".contains(keyEvent.getCharacter())) {
        keyEvent.consume();
      }
    }
  });
  </code></pre>
  
  <p>My question is which is the slandered way to do this? Can anyone help me to pick up the right? </p>