I have created a custom TextField (by extending javafx.scene.control.TextField) which I use to store monetary values eg say $120,000. I have called this class CurrencyField and It has two constructors public CurrencyField() and public CurrencyField(String currency) I also have a public void setCurrency(String currency) method for setting the currency symbol. One can out rightly set the currency by using new CurrencyField(currency) or set it later using the Setter Method - setCurrency(currency)
Usually, I want to be able to use different currency symbols based on conditions (outside the scope of this questions) Eg. I may want to switch the currency from $ to £ by clicking a button. In this case, I want all the CurrencyFields to immediately display the amounts with the new currency symbol.
I have already learned that using Properties and Binding can be used to update one variable when another variable is updated without any extra methods. Now, in a more practical way, I want that if I call the setCurrency("$") method, a field with Rs2,000 will immediately display $2,000.
How can I go about this using Properties and/or Binding or any other way basically?
TextField'stextproperty to display the number AND the currency symbol, I'm afraid this is not possible since any binding of thetextproperty would render theTextFielduneditable (bound properties cannot be set). You need to replace the currency symbols for all text fields without a binding. If the currency stored in a property you could do this from a listener though.