我有两个双重属性price2和price2 。 我知道我可以将它绑定到像这样的标签:
Locale locale = new Locale("en", "UK"); fxLabel.textProperty().bind(Bindings.format("price1/price2: %.3f/%.3f",.price1Property(),price2Property()));但显示的数字没有任何逗号分隔符(即显示123456.789而不是123,456.789)。 理想情况下,我想要做的事情如下:
String pattern = "###,###.###;-###,###.###"; DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale); df.applyPattern(pattern); df.setMinimumFractionDigits(3); df.setMaximumFractionDigits(10); // bind df.format(value from price1 and price 2 property) to the label但我不知道如何在一个属性上做到这一点。 我该如何解决这个问题?
I have two double properties price1 and price2. I know that I can bind it to a label like this:
Locale locale = new Locale("en", "UK"); fxLabel.textProperty().bind(Bindings.format("price1/price2: %.3f/%.3f",.price1Property(),price2Property()));but the displayed number does not have any commas separators (i.e. 123456.789 is shown instead of 123,456.789). Ideally, I would like to be do something like the following:
String pattern = "###,###.###;-###,###.###"; DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(locale); df.applyPattern(pattern); df.setMinimumFractionDigits(3); df.setMaximumFractionDigits(10); // bind df.format(value from price1 and price 2 property) to the labelBut I have no idea how to do this on a property. How can I solve this ?
最满意答案
使用JavaFX高级绑定API,您可以更改格式字符串并将区域设置传递给Binding.format :
Locale locale = new Locale("en", "UK"); fxLabel.textProperty().bind(Bindings.format(locale, "price1/price2: %,.3f/%,.3f", price1Property(), price2Property()));在本例中,','标志用于格式字符串( java.util.Formatter API文档中记录的所有选项和可能性)。
您也可以使用低级绑定API:
StringBinding stringBinding = new StringBinding() { private final static Locale LOCALE = new Locale("en", "UK"); private final static DecimalFormat DF; static { String pattern = "###,###.###;-###,###.###"; DF = (DecimalFormat) NumberFormat.getNumberInstance(LOCALE); DF.applyPattern(pattern); DF.setMinimumFractionDigits(3); DF.setMaximumFractionDigits(10); } public StringBinding() { super.bind(price1Property(), price2Property()); } @Override protected String computeValue() { return "price1/price2 " + DF.format(price1Property().get()) + "/" + DF.format(price2Property().get()); } }; fxLabel.bind(stringBinding);Using the JavaFX high level binding API, you can change the format string and pass the locale to Binding.format:
Locale locale = new Locale("en", "UK"); fxLabel.textProperty().bind(Bindings.format(locale, "price1/price2: %,.3f/%,.3f", price1Property(), price2Property()));In this example, the ',' flag is used in the format string (all options and possibilities documented in the java.util.Formatter API doc.
You can also use the low level binding API:
StringBinding stringBinding = new StringBinding() { private final static Locale LOCALE = new Locale("en", "UK"); private final static DecimalFormat DF; static { String pattern = "###,###.###;-###,###.###"; DF = (DecimalFormat) NumberFormat.getNumberInstance(LOCALE); DF.applyPattern(pattern); DF.setMinimumFractionDigits(3); DF.setMaximumFractionDigits(10); } public StringBinding() { super.bind(price1Property(), price2Property()); } @Override protected String computeValue() { return "price1/price2 " + DF.format(price1Property().get()) + "/" + DF.format(price2Property().get()); } }; fxLabel.bind(stringBinding);更多推荐
发布评论