After reading the docs it is not clear to me the difference between getValue
and getText
for a JFormattedTextField
.
In my code, getText
gives me what I think I need while getValue
always returns null.
It seems to me, based on the docs, that they should both return the same thing at least after the field, when correctly formatted, loses focus.
The getValue
method is supposed to "Returns the last valid value."
A simple explanation would be helpful.
Well JFormattedTextField
is a text component that allows to keep a value and give it a custom String representation (format).
This value is an Object which is typically a Date or Number instance, the two classes with most different formats.
So having said this, getValue() returns the value held by formatted text field component while getText() returns the value's String representation.
For further details on this component please take a look to How to Use Formatted Text Fields:
A formatted text field's text and its value are two different properties, and the value often lags behind the text.
The text property is defined by the JTextField class. This property always reflects what the field displays. The value property, defined by the JFormattedTextField class, might not reflect the latest text displayed in the field. While the user is typing, the text property changes, but the value property does not change until the changes are committed.
To be more precise, the value of a formatted text field can be set by using either the setValue method or the commitEdit method. The setValue method sets the value to the specified argument. The argument can technically be any Object, but the formatter needs to be able to convert it into a string. Otherwise, the text field does not display any substantive information.