Class TextFormatter<V>
- javafx.scene.control.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 intial 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 filter or value converter. If value converter is not provided however, setting a value will result in an IllegalStateException and the value is always null.
Since Formatter contains a value which 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
-
Property Summary
All MethodsInstance MethodsConcrete Methods Type Property and Description ObjectProperty<V>valueThe current value for this formatter.
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classTextFormatter.ChangeContains the state representing a change in the content or selection for a TextInputControl.
-
Field Summary
Fields Modifier and Type Field and Description static StringConverter<String>IDENTITY_STRING_CONVERTERThis string converter converts the text to the same String value.
-
Constructor Summary
Constructors Constructor and Description TextFormatter(StringConverter<V> valueConverter)Creates a new Formatter with the provided value converter.TextFormatter(StringConverter<V> valueConverter, V defaultValue)Creates a new Formatter with the provided value converter and default value.TextFormatter(StringConverter<V> valueConverter, V defaultValue, UnaryOperator<TextFormatter.Change> filter)Creates a new Formatter with the provided filter, value converter and default value.TextFormatter(UnaryOperator<TextFormatter.Change> filter)Creates a new Formatter with the provided filter.
-
Method Summary
All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description UnaryOperator<TextFormatter.Change>getFilter()Filter allows user to intercept and modify any change done to the text content.VgetValue()Gets the value of the property value.StringConverter<V>getValueConverter()The converter between the values and text.voidsetValue(V value)Sets the value of the property value.ObjectProperty<V>valueProperty()The current value for this formatter.
-
Property Detail
-
value
public final ObjectProperty<V> valueProperty
The current value for this formatter. When the formatter is set on aTextInputControland has aSee Also:
-
-
Field Detail
-
IDENTITY_STRING_CONVERTER
public static final StringConverter<String> IDENTITY_STRING_CONVERTER
This string converter converts the text to the same String value. This might be useful for cases where you want to manipulate with the text through the value or you need to provide a default text value.
-
-
Constructor Detail
-
TextFormatter
public TextFormatter(UnaryOperator<TextFormatter.Change> filter)
Creates a new Formatter with the provided filter.Parameters:
filter- The filter to use in this formatter or null
-
TextFormatter
public TextFormatter(StringConverter<V> valueConverter, V defaultValue, UnaryOperator<TextFormatter.Change> filter)Creates a new Formatter with the provided filter, value converter and default value.Parameters:
valueConverter- The value converter to use in this formatter or null.
-
TextFormatter
public TextFormatter(StringConverter<V> valueConverter, V defaultValue)Creates a new Formatter with the provided value converter and default value.Parameters:
valueConverter- The value converter to use in this formatter. This must not be null.
-
TextFormatter
public TextFormatter(StringConverter<V> valueConverter)
Creates a new Formatter with the provided value converter. The default value will be null.Parameters:
valueConverter- The value converter to use in this formatter. This must not be null.
-
-
Method Detail
-
getValueConverter
public final StringConverter<V> getValueConverter()
The converter between the values and text. It maintains a "binding" between theTextInputControl.textProperty()} andvalueProperty()}. The value is updated when the control loses it's focus or it is commited (TextField only). Setting the value will update the text of the control, usin 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
-
getFilter
public final UnaryOperator<TextFormatter.Change> getFilter()
Filter allows user to intercept and modify any change done to the text content.The filter itself is an
UnaryOperatorthat acceptsTextFormatter.Changeobject. It should return aTextFormatter.Changeobject 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()
The current value for this formatter. When the formatter is set on aTextInputControland has aSee Also:
-
setValue
public final void setValue(V value)
Sets the value of the property value.Property description:
The current value for this formatter. When the formatter is set on a
TextInputControland has a
-
getValue
public final V getValue()
Gets the value of the property value.Property description:
The current value for this formatter. When the formatter is set on a
TextInputControland has a
-
© 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.