In my current project, I am storing a currency value as an int which represents the number of cents. However, when the user inputs this value in a text field, they will most likely expect to enter it as a decimal value. Also, I would like to prepend a dollar sign if the user doesn’t enter it manually. How can I leverage a JFormattedTextField to do much of this work for me? Or is there other solutions using pre-existing Swing components so that I don’t have to reinvent the wheel by coding this all by hand?
In my current project, I am storing a currency value as an int which
Share
The
NumberFormatis also suited to format a currency (seeNumberFormat#getCurrencyInstance). And since you need aFormatfor theJFormattedTextFieldI would start there.You can configure the
NumberFormatto allow decimal values, and convert it later on in your code to anintwithout problems