title: How to use JavaFX TextField maxlength
body: <p>How to use this code in my main class of JavaFX so that I can set maxlength of characters in JavaFX <code>TextField</code>?</p>
  <pre><code>class LimitedTextField extends TextField {
  
      private final int limit;
  
      public LimitedTextField(int limit) {
          this.limit = limit;
      }
  
      @Override
      public void replaceText(int start, int end, String text) {
          super.replaceText(start, end, text);
          verify();
      }
  
      @Override
      public void replaceSelection(String text) {
          super.replaceSelection(text);
          verify();
      }
  
      private void verify() {
          if (getText().length() &gt; limit) {
              setText(getText().substring(0, limit));
          }
  
      }
  };
  </code></pre>
  <p>My JavaFX main class is given below:</p>
  <pre><code>public class TextFiled extends Application {
      @Override
      public void start(Stage primaryStage) {
          final TextField t_fname = new TextField();
          
          StackPane root = new StackPane();
          root.getChildren().add(t_fname);
          
          Scene scene = new Scene(root, 300, 250);
          
          primaryStage.setTitle(&quot;Hello World!&quot;);
          primaryStage.setScene(scene);
          primaryStage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>