/** * Adds a text formatter to the text field that only allows adding integers */ public static void applyIntegerFormatter(@Nonnull TextField textField, @Nonnull IntegerProperty integerProperty, @Nonnull Predicate<Integer> filter, @Nonnull NumberStringConverterForIntegers converter) { textField.setTextFormatter(new TextFormatter<>(converter, integerProperty.get(), new IntegerTextFormatterFilter(converter, filter))); }
private static void setupTextField(TextField textField, Consumer<String> converter) { textField.setTextFormatter(new TextFormatter<>(change -> { try { if (!Strings.isNullOrEmpty(change.getControlNewText())) { converter.accept(change.getControlNewText()); } return change; } catch (Exception e) { return null; } })); }
/** * Creates the delegate. * * @param component the component */ public FxComboBoxDelegate(FxComboBox<?> component) { this.component = component; component.getEditor().setTextFormatter(new TextFormatter<>(this)); }
/** * Adds a text formatter to the text field that only allows adding doubles */ public static void applyDoubleFormatter(@Nonnull TextField textField, @Nonnull DoubleProperty doubleProperty, @Nonnull Predicate<Double> filter, @Nonnull NumberStringConverterForFloatingPointNumbers converter) { textField.setTextFormatter(new TextFormatter<>(converter, doubleProperty.get(), new DoubleTextFormatterFilter(converter, filter))); }
public class PersonView implements Initializable { @FXML TextField name; @FXML TextField age; @FXML Button ok; @Override public void initialize(URL location, ResourceBundle resources) { PersonViewModel viewModel = new PersonViewModel(); UnaryOperator<Change> filter = change -> { if (viewModel.validAgeInput(change.getControlNewText()) { // accept return change ; } else { // reject return null ; } }; TextFormatter<Integer> ageFormatter = new TextFormatter<>(new IntegerStringConverter(), 0, filter); age.setTextFormatter(ageFormatter); ageFormatter.valueProperty().bindBidirectional(viewModel.ageProperty().asObject()); name.textProperty().bindBidirectional(viewModel.nameProperty()); } }
/** * Creates the delegate. * * @param component the component */ public FxDatePickerDelegate(FxDatePicker component) { this.component = component; component.setConverter(new DatePickerConverter()); component.getEditor().setTextFormatter(new TextFormatter<>(this)); }
}; TextFormatter<String> textFormatter = new TextFormatter<>(filter); textField.setTextFormatter(textFormatter);
public void initialize(LidarScanLoggerController loggerController, LidarScanLogViewer lidarScanLogViewer) { this.lidarScanLogViewer = lidarScanLogViewer; logDataToggleButton.selectedProperty().bindBidirectional(loggerController.writingProperty()); logDataToggleButton.selectedProperty().addListener( (ChangeListener<Boolean>) (observable, oldValue, newValue) -> logDataToggleButton.setText(newValue ? "Stop Writing" : "Start Writing")); connectToggleButton.selectedProperty().bindBidirectional(loggerController.enableNetworkProcessorClientProperty()); connectToggleButton.selectedProperty() .addListener((ChangeListener<Boolean>) (observable, oldValue, newValue) -> connectToggleButton.setText(newValue ? "Disconnect" : "Connect")); // logDataToggleButton.selectedProperty().bindBidirectional(connectToggleButton.disableProperty()); // connectToggleButton.selectedProperty().bindBidirectional(logDataToggleButton.disableProperty()); networkProcessorIPAdTextField.setTextFormatter(TextFormatterTools.ipAddressTextFormatter()); networkProcessorIPAdTextField.setText(loggerController.networkProcessorAddressProperty().get()); }
public void initialize(LidarScanLoggerController loggerController, LidarScanLogViewer lidarScanLogViewer) { this.lidarScanLogViewer = lidarScanLogViewer; logDataToggleButton.selectedProperty().bindBidirectional(loggerController.writingProperty()); logDataToggleButton.selectedProperty().addListener( (ChangeListener<Boolean>) (observable, oldValue, newValue) -> logDataToggleButton.setText(newValue ? "Stop Writing" : "Start Writing")); connectToggleButton.selectedProperty().bindBidirectional(loggerController.enableNetworkProcessorClientProperty()); connectToggleButton.selectedProperty() .addListener((ChangeListener<Boolean>) (observable, oldValue, newValue) -> connectToggleButton.setText(newValue ? "Disconnect" : "Connect")); // logDataToggleButton.selectedProperty().bindBidirectional(connectToggleButton.disableProperty()); // connectToggleButton.selectedProperty().bindBidirectional(logDataToggleButton.disableProperty()); networkProcessorIPAdTextField.setTextFormatter(TextFormatterTools.ipAddressTextFormatter()); networkProcessorIPAdTextField.setText(loggerController.networkProcessorAddressProperty().get()); }
@Override protected void setupDialogControls() { // setup binding between view model and the UI controls dpDate.valueProperty().bindBidirectional(noteViewModel.date); // use text formatter for time values => makes sure that the value is also valid final TextFormatter<LocalTime> timeTextFormatter = new TextFormatter<>(new TimeToStringConverter()); timeTextFormatter.valueProperty().bindBidirectional(noteViewModel.time); tfTime.setTextFormatter(timeTextFormatter); taText.textProperty().bindBidirectional(noteViewModel.comment); FxWorkarounds.fixDatePickerTextEntry(dpDate); // setup validation of the UI controls // => due to a ControlsFX bug the validation setup must be executed after the dialog has been shown // (see https://bitbucket.org/controlsfx/controlsfx/issues/539/multiple-dialog-fields-with-validation ) Platform.runLater(() -> { validationSupport.registerValidator(dpDate, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.note.error.date"))); validationSupport.registerValidator(tfTime, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.note.error.time"))); validationSupport.registerValidator(taText, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.note.error.no_text"))); }); }
@Override protected void setupDialogControls() { laWeightUnit.setText(context.getFormatUtils().getWeightUnitName()); // setup binding between view model and the UI controls dpDate.valueProperty().bindBidirectional(weightViewModel.date); // use text formatter for time values => makes sure that the value is also valid final TextFormatter<LocalTime> timeTextFormatter = new TextFormatter<>(new TimeToStringConverter()); timeTextFormatter.valueProperty().bindBidirectional(weightViewModel.time); tfTime.setTextFormatter(timeTextFormatter); tfValue.textProperty().bindBidirectional(weightViewModel.value, new NumberStringConverter()); taComment.textProperty().bindBidirectional(weightViewModel.comment); FxWorkarounds.fixDatePickerTextEntry(dpDate); // setup validation of the UI controls // => due to a ControlsFX bug the validation setup must be executed after the dialog has been shown // (see https://bitbucket.org/controlsfx/controlsfx/issues/539/multiple-dialog-fields-with-validation ) Platform.runLater(() -> { validationSupport.registerValidator(dpDate, Validator.createEmptyValidator(context.getResources().getString("st.dlg.weight.error.date"))); validationSupport.registerValidator(tfTime, // Validator.createEmptyValidator(context.getResources().getString("st.dlg.weight.error.time"))); validationSupport.registerValidator(tfValue, true, (Control control, String newValue) -> ValidationResult .fromErrorIf(tfValue, context.getResources().getString("st.dlg.weight.error.weight"), !ValidationUtils.isValueDoubleBetween(newValue, 0.1d, 1000))); }); }
/** * Setup of the binding between view model and the UI controls. */ private void setupBinding() { dpDate.valueProperty().bindBidirectional(exerciseViewModel.date); // use comment formatter for time values => makes sure that the value is also valid final TextFormatter<LocalTime> timeTextFormatter = new TextFormatter<>(new TimeToStringConverter()); timeTextFormatter.valueProperty().bindBidirectional(exerciseViewModel.time); tfTime.setTextFormatter(timeTextFormatter); cbSportType.valueProperty().bindBidirectional(exerciseViewModel.sportType); cbSportSubtype.valueProperty().bindBidirectional(exerciseViewModel.sportSubType); cbIntensity.valueProperty().bindBidirectional(exerciseViewModel.intensity); tfDistance.textProperty().bindBidirectional(exerciseViewModel.distance, new NumberStringConverter()); tfAvgSpeed.textProperty().bindBidirectional(exerciseViewModel.avgSpeed); tfDuration.textProperty().bindBidirectional(exerciseViewModel.duration, new TimeInSecondsToStringConverter()); tfAscent.textProperty().bindBidirectional(exerciseViewModel.ascent, new NumberStringConverter()); tfDescent.textProperty().bindBidirectional(exerciseViewModel.descent, new NumberStringConverter()); tfAvgHeartrate.textProperty().bindBidirectional(exerciseViewModel.avgHeartRate, new NumberStringConverter()); tfCalories.textProperty().bindBidirectional(exerciseViewModel.calories, new NumberStringConverter()); cbEquipment.valueProperty().bindBidirectional(exerciseViewModel.equipment); tfHrmFile.textProperty().bindBidirectional(exerciseViewModel.hrmFile); taComment.textProperty().bindBidirectional(exerciseViewModel.comment); }