Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Text in a JFormattedTextField

Tags:

java

I'm using the code snippet below to create a JFormattedTextField. When entering values via the GUI text field the formatting works as expected. However, when I programmatically set the value the formatting does not occur. How can I force this to occur?

JFormattedTextField myTextField = new JFormattedTextField(new DecimalFormat("#0.###"));
// Formatting Does Not Occur
myTextField.setText("555.55555");
like image 381
javacavaj Avatar asked Jan 22 '23 22:01

javacavaj


2 Answers

Take a look at the setValue() method

Try this

myTextField.setValue(new Float("555.55555"));
like image 145
Ovi Tisler Avatar answered Jan 25 '23 13:01

Ovi Tisler


To add to OTisler's answer:

From the Javadoc for JFormattedTextField.setText()

Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.

From the Javadoc for JFormattedTextField.setValue()

Sets the value that will be formatted by an AbstractFormatter obtained from the current AbstractFormatterFactory.

like image 25
Kelly S. French Avatar answered Jan 25 '23 13:01

Kelly S. French