@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX BigDecimalField Demo");
ObjectProperty<DateFormat> dateFormatProperty = new SimpleObjectProperty<>(DateFormat.getDateInstance());
ObjectProperty<NumberFormat> numberFormatProperty = new SimpleObjectProperty<>(NumberFormat.getNumberInstance());
GridPane root = new GridPane();
root.setHgap(10);
root.setVgap(10);
root.setPadding(new Insets(10, 10, 10, 10));
final BigDecimalField defaultSpinner = new BigDecimalField();
final BigDecimalField decimalFormat = new BigDecimalField(BigDecimal.ZERO, new BigDecimal("0.05"), new DecimalFormat("#,##0.00"));
final BigDecimalField percent = new BigDecimalField(BigDecimal.ZERO, new BigDecimal("0.01"), NumberFormat.getPercentInstance());
final BigDecimalField localizedCurrency = new BigDecimalField(BigDecimal.ZERO, new BigDecimal("0.01"), NumberFormat.getCurrencyInstance(Locale.UK));
final BigDecimalField promptText = new BigDecimalField();
promptText.setNumber(null);
promptText.setPromptText("Enter something");
int rowIndex = 1;
root.addRow(rowIndex++, new Label("default"), defaultSpinner);
root.addRow(rowIndex++, new Label("custom decimal format"), decimalFormat);
root.addRow(rowIndex++, new Label("percent"), percent);
root.addRow(rowIndex++, new Label("localized currency"), localizedCurrency);
final BigDecimalField disabledField = new BigDecimalField();
disabledField.setDisable(true);
root.addRow(rowIndex++, new Label("disabled field"), disabledField);
root.addRow(rowIndex++, new Label("regular TextField"), new TextField("1.000,1234"));
root.addRow(rowIndex++, new Label("with promptText"), promptText);
CalendarTextField calendarTextField = new CalendarTextField();
root.addRow(rowIndex++, new Label("CalendarTextField"), calendarTextField);
ComboBox<Locale> cmbLocales = new ComboBox<>(FXCollections.observableArrayList(Locale.GERMANY, Locale.UK, Locale.FRANCE));
cmbLocales.setOnAction(event -> {
dateFormatProperty.set(DateFormat.getDateInstance(DateFormat.MEDIUM, cmbLocales.getValue()));
numberFormatProperty.set(NumberFormat.getNumberInstance(cmbLocales.getValue()));
});
root.addRow(rowIndex++, new Label("Locale"), cmbLocales);
root.addRow(rowIndex++, new Label("Field with boundaries (0,100%)"),
BigDecimalFieldBuilder.create()
.number(new BigDecimal("0.1"))
.minValue(BigDecimal.ZERO)
.maxValue(BigDecimal.ONE)
.stepwidth(new BigDecimal("0.01"))
.format(DecimalFormat.getPercentInstance())
.build()
);
promptText.numberProperty().addListener(new ChangeListener<BigDecimal>() {
@Override
public void changed(ObservableValue<? extends BigDecimal> observableValue, BigDecimal o, BigDecimal o1) {
System.out.println(o1);
}
});
Button button = new Button("Reset fields");
button.setOnAction((ActionEvent event) -> {
defaultSpinner.setNumber(new BigDecimal(Math.random() * 1000));
decimalFormat.setNumber(new BigDecimal(Math.random() * 1000));
percent.setNumber(new BigDecimal(Math.random()));
localizedCurrency.setNumber(new BigDecimal(Math.random() * 1000));
disabledField.setNumber(new BigDecimal(Math.random() * 1000));
promptText.setNumber(null);
calendarTextField.setCalendar(Calendar.getInstance());
decimalFormat.requestFocus();
});
root.addRow(rowIndex++, new Label(), button);
BigDecimalLabel bigDecimalLabel = new BigDecimalLabel();
bigDecimalLabel.numberProperty().bind(defaultSpinner.numberProperty());
bigDecimalLabel.formatProperty().bind(numberFormatProperty);
root.addRow(rowIndex++, new Label("BigDecimalLabel"), bigDecimalLabel);
CalendarLabel calendarLabel = new CalendarLabel();
calendarLabel.valueProperty().bind(calendarTextField.calendarProperty());
calendarLabel.formatProperty().bind(dateFormatProperty);
root.addRow(rowIndex++, new Label("CalendarLabel"), calendarLabel);
// defaultSpinner.requestFocus();
// calendarTextField.requestFocus();
Platform.runLater(new Runnable() {
@Override
public void run() {