I'm working on javafx application and i have noticed, that some CSSstyle/double functions not working properly, when Windows regional settings have been changed (i am from Estonia). When i open program window, some field are disabled (comboboxes and datepickers): Disable code:
public void lockElements() {
policyTypeCombo.setDisable(true);
insurerCombo.setDisable(true);
clientCombo.setDisable(true);
startDate.setDisable(true);
}
After I click Unlock button this code works:
@FXML
public void unlockElements() {
policyTypeCombo.setDisable(false);
insurerCombo.setDisable(false);
startDate.setDisable(false);
}
It is all very simple, and in my IntellijIdea everything works fine: Also when i enter some Double in Sum text field, it should fill part1-12 sums automatically: Here is code, that allows enter only double:
sumText.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\d{0,7}([\\.]\\d{0,4})?")) {
sumText.setText(oldValue);
}
}
});
Here is part of the code, that fills other sums
double sum = Double.valueOf(sumText.getText());
double part = sum / 12d;
part1Sum.setText(String.format("%.2f", part));
part2Sum.setText(String.format("%.2f", part));
part3Sum.setText(String.format("%.2f", part));
part4Sum.setText(String.format("%.2f", part));
part5Sum.setText(String.format("%.2f", part));
part6Sum.setText(String.format("%.2f", part));
part7Sum.setText(String.format("%.2f", part));
part8Sum.setText(String.format("%.2f", part));
part9Sum.setText(String.format("%.2f", part));
part10Sum.setText(String.format("%.2f", part));
part11Sum.setText(String.format("%.2f", part));
part12Sum.setText(String.format("%.2f", part));
After packing my app and installing on my computer, everything works fine. BUT on computers with different regional settings i have this picture: after clicking Unlock button, comboboxes and datepickers are still "light-gray"(but working, i can choose other options): And when I enter new sum, 1-12 parts sums don't fill automatically. As i understand, it is related somehow to Reginal decimal symbols or something like this. Question : how i can make my App independent from machine regional setting?
Thank you everyone, thank you @Slaw. I solve this problem by using BigDecimal. Code was:
double sum = Double.valueOf(sumText.getText());
double part = sum / 12d;
part1Sum.setText(String.format("%.2f", part));
part2Sum.setText(String.format("%.2f", part));
part3Sum.setText(String.format("%.2f", part));
part4Sum.setText(String.format("%.2f", part));
part5Sum.setText(String.format("%.2f", part));
part6Sum.setText(String.format("%.2f", part));
part7Sum.setText(String.format("%.2f", part));
part8Sum.setText(String.format("%.2f", part));
part9Sum.setText(String.format("%.2f", part));
part10Sum.setText(String.format("%.2f", part));
part11Sum.setText(String.format("%.2f", part));
part12Sum.setText(String.format("%.2f", part));
I changed it to:
int scale = 2;
double sum = BigDecimal.valueOf(Double.valueOf(sumText.getText()) / 12d).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
part1Sum.setText(Double.toString(sum));
part2Sum.setText(Double.toString(sum));
part3Sum.setText(Double.toString(sum));
part4Sum.setText(Double.toString(sum));
part5Sum.setText(Double.toString(sum));
part6Sum.setText(Double.toString(sum));
part7Sum.setText(Double.toString(sum));
part8Sum.setText(Double.toString(sum));
part9Sum.setText(Double.toString(sum));
part10Sum.setText(Double.toString(sum));
part11Sum.setText(Double.toString(sum));
part12Sum.setText(Double.toString(sum));
Now everything is OK.