Override replaceText and replaceSelection to create customized TextField : TextField « JavaFX « Java






Override replaceText and replaceSelection to create customized TextField

  

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        
        TextField field = new TextField() {
          @Override public void replaceText(int start, int end, String text) {
              if (!text.matches("[a-z]")) {
                  super.replaceText(start, end, text);
              }
          }
       
          @Override public void replaceSelection(String text) {
              if (!text.matches("[a-z]")) {
                  super.replaceSelection(text);
              }
          }
      };
        
        root.getChildren().add(field); 
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

   
    
  








Related examples in the same category

1.Set and clear value for TextField
2.Adding ContextMenu to TextField
3.Binding string value from TextField to Stage Title
4.set Prompt Text For TextField