Class TextFormatter<V>
- Type Parameters:
-
V- The type of the value
public class TextFormatter<V> extends Object
TextInputControl text by using two distinct mechanisms: - A filter (
getFilter()) that can intercept and modify user input. This helps to keep the text in the desired format. A default text supplier can be used to provide the initial text. - A value converter (
getValueConverter()) and value (valueProperty()) can be used to provide special format that represents a value of typeV. If the control is editable and the text is changed by the user, the value is then updated to correspond to the text.
It's possible to have a formatter with just a filter or a value converter. If a value converter is not provided, setting a value will result in an IllegalStateException and the value is always null.
Since Formatter contains a value that represents the state of the TextInputControl to which it is currently assigned, a single Formatter instance can be used only in one TextInputControl at a time.
- Since:
- JavaFX 8u40
Nested Class Summary
| Modifier and Type | Class | Description |
|---|---|---|
static final class |
TextFormatter.Change |
Contains the state representing a change in the content or selection for a TextInputControl. |
Property Summary
| Type | Property | Description |
|---|---|---|
final ObjectProperty |
value |
The current value for this formatter. |
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
static final StringConverter |
IDENTITY_STRING_CONVERTER |
This string converter converts the text to the same String value. |
Constructor Summary
| Constructor | Description |
|---|---|
TextFormatter |
Creates a new Formatter with the provided filter. |
TextFormatter |
Creates a new Formatter with the provided value converter. |
TextFormatter |
Creates a new Formatter with the provided value converter and default value. |
TextFormatter |
Creates a new Formatter with the provided filter, value converter and default value. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
final UnaryOperator |
getFilter() |
Filter allows user to intercept and modify any change done to the text content. |
final V |
getValue() |
Gets the value of the value property. |
final StringConverter |
getValueConverter() |
The converter between the values and text. |
final void |
setValue |
Sets the value of the value property. |
final ObjectProperty |
valueProperty() |
The current value for this formatter. |
Property Details
value
public final ObjectProperty<V> valueProperty
TextInputControl and has a valueConverter, the value is set by the control, when the text is committed.- See Also:
Field Details
IDENTITY_STRING_CONVERTER
public static final StringConverter<String> IDENTITY_STRING_CONVERTER
Constructor Details
TextFormatter
public TextFormatter(UnaryOperator<TextFormatter.Change> filter)
- Parameters:
-
filter- The filter to use in this formatter or null
TextFormatter
public TextFormatter(StringConverter<V> valueConverter, V defaultValue, UnaryOperator<TextFormatter.Change> filter)
- Parameters:
-
valueConverter- The value converter to use in this formatter or null. -
defaultValue- the default value. -
filter- The filter to use in this formatter or null
TextFormatter
public TextFormatter(StringConverter<V> valueConverter, V defaultValue)
- Parameters:
-
valueConverter- The value converter to use in this formatter. This must not be null. -
defaultValue- the default value
TextFormatter
public TextFormatter(StringConverter<V> valueConverter)
- Parameters:
-
valueConverter- The value converter to use in this formatter. This must not be null.
Method Details
getValueConverter
public final StringConverter<V> getValueConverter()
TextInputControl.textProperty() } and valueProperty() }. The value is updated when the control loses it's focus or it is committed (TextField only). Setting the value will update the text of the control, using the provided converter. If it's impossible to convert text to value, an exception should be thrown.- Returns:
- StringConverter for values or null if none provided
- See Also:
getFilter
public final UnaryOperator<TextFormatter.Change> getFilter()
The filter itself is an UnaryOperator that accepts TextFormatter.Change object. It should return a TextFormatter.Change object that contains the actual (filtered) change. Returning null rejects the change.
- Returns:
- the filter for this formatter or null if there is none
valueProperty
public final ObjectProperty<V> valueProperty()
TextInputControl and has a valueConverter, the value is set by the control, when the text is committed.- Returns:
- the
valueproperty - See Also:
setValue
public final void setValue(V value)
value property.- Property description:
- The current value for this formatter. When the formatter is set on a
TextInputControland has avalueConverter, the value is set by the control, when the text is committed. - Parameters:
-
value- the value for thevalueproperty - See Also:
getValue
public final V getValue()
value property.- Property description:
- The current value for this formatter. When the formatter is set on a
TextInputControland has avalueConverter, the value is set by the control, when the text is committed. - Returns:
- the value of the
valueproperty - See Also:
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.