TextField field = new TextField(); TextFormatter fieldFormatter = new TextFormatter( TextFormatter.IDENTITY_STRING_CONVERTER, "initial"); field.setTextFormatter(fieldFormatter); fieldFormatter.valueProperty().addListener((s, ov, nv) -> { // do stuff that needs to be done on commit } );
TextField numberField = new TextField(); numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
TextField textField = new TextField(); TextFormatter<String> formatter = new TextFormatter<String>( change -> { change.setText(change.getText().replaceAll("[^a-zA-Z0-9]", "")); return change; }); textField.setTextFormatter(formatter);
TextField textField = new TextField(); textField.setTextFormatter(new TextFormatter<String>((Change change) -> { String newText = change.getControlNewText(); if (newText.length() > 1) { return null ; } else { return change ; } });
TextField tf = new TextField(); SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); tf.setTextFormatter(new TextFormatter<>(new DateTimeStringConverter(format), format.parse("00:00:00")));
DecimalFormat format = new DecimalFormat( "#.0" ); TextField field = new TextField(); field.setTextFormatter( new TextFormatter<>(c -> { if ( c.getControlNewText().isEmpty() ) { return c; } ParsePosition parsePosition = new ParsePosition( 0 ); Object object = format.parse( c.getControlNewText(), parsePosition ); if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() ) { return null; } else { return c; } }));
DecimalFormat format = new DecimalFormat( "#.0" ); TextField field = new TextField(); field.setTextFormatter( new TextFormatter<>(c -> { if ( c.getControlNewText().isEmpty() ) { return c; } ParsePosition parsePosition = new ParsePosition( 0 ); Object object = format.parse( c.getControlNewText(), parsePosition ); if ( object == null || parsePosition.getIndex() < c.getControlNewText().length() ) { return null; } else { return c; } }));
}); textField.setTextFormatter(textFormatter);
TextField summand1 = new TextField(); TextField summand2 = new TextField(); TextField result = new TextField(); StringConverter<Double> converter = new DoubleStringConverter(); TextFormatter<Double> tf1 = new TextFormatter<>(converter, 0d); TextFormatter<Double> tf2 = new TextFormatter<>(converter, 0d); TextFormatter<Double> tfRes = new TextFormatter<>(converter, 0d); summand1.setTextFormatter(tf1); summand2.setTextFormatter(tf2); result.setTextFormatter(tfRes); tfRes.valueProperty().bind( Bindings.createObjectBinding(() -> tf1.getValue() + tf2.getValue(), tf1.valueProperty(), tf2.valueProperty())); result.setEditable(false);
@Override public void start(Stage primaryStage) { Rectangle rect = new Rectangle(100, 100); StringConverter<Double> converter = new DoubleStringConverter(); TextField xTextField = new TextField(); TextFormatter<Double> xFromatter = new TextFormatter<>(converter); xTextField.setTextFormatter(xFromatter); TextField widthTextField = new TextField(); TextFormatter<Double> widthFromatter = new TextFormatter<Double>(converter); widthTextField.setTextFormatter(widthFromatter); xFromatter.valueProperty().bindBidirectional(rect.xProperty().asObject()); widthFromatter.valueProperty().bindBidirectional(rect.widthProperty().asObject()); Scene scene = new Scene(new VBox(10, xTextField, widthTextField, new Pane(rect)), 500, 500); primaryStage.setScene(scene); primaryStage.show(); }
TextField numberField = new TextField(); numberField.setAlignment(Pos.CENTER_RIGHT); numberField.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));
public void start(Stage primaryStage) { TextField tf = new TextField(); tf.setTextFormatter(new TextFormatter<String>((Change c) -> { String text = c.getText(); int oldAnchor = c.getAnchor();
int len = 20; TextField field = new TextField("max chars: " + len ); // here we reject any change which exceeds the length UnaryOperator<Change> rejectChange = c -> { // check if the change might effect the validating predicate if (c.isContentChange()) { // check if change is valid if (c.getControlNewText().length() > len) { // invalid change // sugar: show a context menu with error message final ContextMenu menu = new ContextMenu(); menu.getItems().add(new MenuItem("This field takes\n"+len+" characters only.")); menu.show(c.getControl(), Side.BOTTOM, 0, 0); // return null to reject the change return null; } } // valid change: accept the change by returning it return c; }; field.setTextFormatter(new TextFormatter(rejectChange));
for (int i = 0; i < num; i++) { final TextField tf = new TextField(); tf.setTextFormatter(new TextFormatter(filter)); fields.add(tf);
public void start(Stage primaryStage) { TextField textField = new TextField(); textField.setTextFormatter(new TextFormatter<String>(change -> {
tf.setTextFormatter(formatter); btn.disableProperty().bind(IntegerExpression.integerExpression(formatter.valueProperty()).lessThan(0));
textField.setTextFormatter(formatter);
field.setTextFormatter( new TextFormatter<>( c ->
number.setTextFormatter(formatter);
textField.setTextFormatter(new TextFormatter<Integer>(c -> { if (c.getControlNewText().matches("\\d?")) { return c ;