Top Description Inners Fields Constructors Methods
javafx.scene.control

public Class TextFormatter<V>

extends Object
Class Inheritance
Type Parameters
<V>
The type of the value
Imports
com.sun.javafx.scene.control.FormatterAccessor, javafx.beans.NamedArg, javafx.beans.property.ObjectProperty, .ObjectPropertyBase, javafx.util.StringConverter, java.util.function.Consumer, .UnaryOperator

A Formatter describes a format of a TextInputControl text by using two distinct mechanisms:

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 and Inner Type Summary

Modifier and TypeClass and Description
public static class
TextFormatter.Change

Contains the state representing a change in the content or selection for a TextInputControl.

Field Summary

Modifier and TypeField and Description
private final UnaryOperator<TextFormatter.Change>
public static final StringConverter<String>
IDENTITY_STRING_CONVERTER

This string converter converts the text to the same String value.

private Consumer<TextFormatter<?>>
private final ObjectProperty<V>
value

The current value for this formatter.

private final StringConverter<V>

Constructor Summary

AccessConstructor and Description
public
TextFormatter(UnaryOperator<TextFormatter.Change>
The filter to use in this formatter or null
filter
)

Creates a new Formatter with the provided filter.

public
TextFormatter(StringConverter<V>
The value converter to use in this formatter or null.
valueConverter
,
V
the default value.
defaultValue
,
UnaryOperator<TextFormatter.Change>
The filter to use in this formatter or null
filter
)

Creates a new Formatter with the provided filter, value converter and default value.

public
TextFormatter(StringConverter<V>
The value converter to use in this formatter. This must not be null.
valueConverter
,
V
the default value
defaultValue
)

Creates a new Formatter with the provided value converter and default value.

public
TextFormatter(StringConverter<V>
The value converter to use in this formatter. This must not be null.
valueConverter
)

Creates a new Formatter with the provided value converter.

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public final UnaryOperator<TextFormatter.Change>

Returns:

the filter for this formatter or null if there is none
getFilter
()

Filter allows user to intercept and modify any change done to the text content.

public final V
public final StringConverter<V>

Returns:

StringConverter for values or null if none provided
getValueConverter
()

The converter between the values and text.

public final void
setValue(V value)

pack-priv void
private void
pack-priv void
public final ObjectProperty<V>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

filterback to summary
private final UnaryOperator<TextFormatter.Change> filter
IDENTITY_STRING_CONVERTERback to summary
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.

textUpdaterback to summary
private Consumer<TextFormatter<?>> textUpdater
valueback to summary
private final ObjectProperty<V> value

The current value for this formatter. When the formatter is set on a TextInputControl and has a valueConverter, the value is set by the control, when the text is committed.

valueConverterback to summary
private final StringConverter<V> valueConverter

Constructor Detail

TextFormatterback to summary
public TextFormatter(UnaryOperator<TextFormatter.Change> filter)

Creates a new Formatter with the provided filter.

Parameters
filter:UnaryOperator<TextFormatter.Change>

The filter to use in this formatter or null

TextFormatterback to summary
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:StringConverter<V>

The value converter to use in this formatter or null.

defaultValue:V

the default value.

filter:UnaryOperator<TextFormatter.Change>

The filter to use in this formatter or null

TextFormatterback to summary
public TextFormatter(StringConverter<V> valueConverter, V defaultValue)

Creates a new Formatter with the provided value converter and default value.

Parameters
valueConverter:StringConverter<V>

The value converter to use in this formatter. This must not be null.

defaultValue:V

the default value

TextFormatterback to summary
public TextFormatter(StringConverter<V> valueConverter)

Creates a new Formatter with the provided value converter. The default value will be null.

Parameters
valueConverter:StringConverter<V>

The value converter to use in this formatter. This must not be null.

Method Detail

bindToControlback to summary
pack-priv void bindToControl(Consumer<TextFormatter<?>> updater)
getFilterback to summary
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 UnaryOperator that accepts javafx.scene.control.TextFormatter.Change object. It should return a javafx.scene.control.TextFormatter.Change object that contains the actual (filtered) change. Returning null rejects the change.

Returns:UnaryOperator<TextFormatter.Change>

the filter for this formatter or null if there is none

getValueback to summary
public final V getValue()
getValueConverterback to summary
public final StringConverter<V> getValueConverter()

The converter between the values and text. It maintains a "binding" between the javafx.scene.control.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<V>

StringConverter for values or null if none provided

See Also
javafx.scene.control.TextField#commitValue(), javafx.scene.control.TextField#cancelEdit()
setValueback to summary
public final void setValue(V value)
unbindFromControlback to summary
pack-priv void unbindFromControl()
updateTextback to summary
private void updateText()
updateValueback to summary
pack-priv void updateValue(String text)
valuePropertyback to summary
public final ObjectProperty<V> valueProperty()
javafx.scene.control back to summary

public final Class TextFormatter.Change

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable

Contains the state representing a change in the content or selection for a TextInputControl. This object is passed to any registered formatter on the TextInputControl whenever the text for the TextInputControl is modified.

This class contains state and convenience methods for determining what change occurred on the control. It also has a reference to the TextInputControl itself so that the developer may query any other state on the control. Note that you should never modify the state of the control directly from within the formatter handler.

The Change of the text is described by range (getRangeStart(), getRangeEnd()) and text (getText(). There are 3 cases that can occur:

The Change is mutable, but not observable. It should be used only for the life of a single change. It is intended that the Change will be modified from within the formatter.

Since
JavaFX 8u40

Field Summary

Modifier and TypeField and Description
private final FormatterAccessor
pack-priv int
pack-priv int
private Control
pack-priv int
pack-priv int
pack-priv String

Constructor Summary

AccessConstructor and Description
pack-priv
Change(Control control, FormatterAccessor accessor, int anchor, int caret)

pack-priv
Change(Control control, FormatterAccessor accessor, int start, int end, String text)

pack-priv
Change(Control control, FormatterAccessor accessor, int start, int end, String text, int anchor, int caret)

Method Summary

Modifier and TypeMethod and Description
public TextFormatter.Change
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public final int

Returns:

The new anchor position
getAnchor
()

Gets the new anchor.

public final int

Returns:

The new caret position
getCaretPosition
()

Gets the new caret position.

public final Control

Returns:

The control associated with this change. This will never be null.
getControl
()

Gets the control associated with this change.

public final int

Returns:

The previous anchor
getControlAnchor
()

Gets the current anchor position of the control.

public final int

Returns:

The previous caret position
getControlCaretPosition
()

Gets the current caret position of the control.

public final String

Returns:

The controls proposed new text at the time of this call, according to the state set for start, end, and text properties on this Change object.
getControlNewText
()

Gets the complete new text which will be used on the control after this change.

public final String

Returns:

the previous text of control
getControlText
()

This is the full text that control has before the change.

public final int

Returns:

The end index
getRangeEnd
()

Gets the end index into the TextInputControl#getText() for the modification.

public final int

Returns:

The start index
getRangeStart
()

Gets the start index into the TextInputControl#getText() for the modification.

public final IndexRange

Returns:

The selected range of this change.
getSelection
()

Gets the selection of this change.

public final String

Returns:

The text involved in this change. This will never be null.
getText
()

Gets the text used in this change.

public final boolean

Returns:

true if text was being added
isAdded
()

Gets whether this change was in response to text being added.

public final boolean

Returns:

true if the content changed
isContentChange
()

The content change is any of add, delete or replace changes.

public final boolean

Returns:

true if text was being deleted
isDeleted
()

Gets whether this change was in response to text being deleted.

public final boolean

Returns:

true if text was being replaced
isReplaced
()

Gets whether this change was in response to text being replaced.

public final void
selectRange(int
The new anchor position
newAnchor
,
int
The new caret position
newCaretPosition
)

Sets the selection.

public final void
setAnchor(int
The new anchor position
newAnchor
)

Sets the anchor.

public final void
setCaretPosition(int
The new caret position
newCaretPosition
)

Sets the caret position.

public final void
setRange(int
The new start value. Must be a valid start value
start
,
int
The new end value. Must be a valid end value
end
)

A method assigning both the start and end values together, in such a way as to ensure they are valid with respect to each other.

public final void
setText(String
The text. This cannot be null.
value
)

Sets the text to use in this change.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
equalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

accessorback to summary
private final FormatterAccessor accessor
anchorback to summary
pack-priv int anchor
caretback to summary
pack-priv int caret
controlback to summary
private Control control
endback to summary
pack-priv int end
startback to summary
pack-priv int start
textback to summary
pack-priv String text

Constructor Detail

Changeback to summary
pack-priv Change(Control control, FormatterAccessor accessor, int anchor, int caret)
Changeback to summary
pack-priv Change(Control control, FormatterAccessor accessor, int start, int end, String text)
Changeback to summary
pack-priv Change(Control control, FormatterAccessor accessor, int start, int end, String text, int anchor, int caret)

Method Detail

cloneback to summary
public TextFormatter.Change clone()

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Returns:TextFormatter.Change

a clone of this instance.

Annotations
@Override
getAnchorback to summary
public final int getAnchor()

Gets the new anchor. This value will always be > 0 and <= getControlNewText().getLength()}

Returns:int

The new anchor position

getCaretPositionback to summary
public final int getCaretPosition()

Gets the new caret position. This value will always be > 0 and <= getControlNewText().getLength()}

Returns:int

The new caret position

getControlback to summary
public final Control getControl()

Gets the control associated with this change.

Returns:Control

The control associated with this change. This will never be null.

getControlAnchorback to summary
public final int getControlAnchor()

Gets the current anchor position of the control.

Returns:int

The previous anchor

getControlCaretPositionback to summary
public final int getControlCaretPosition()

Gets the current caret position of the control.

Returns:int

The previous caret position

getControlNewTextback to summary
public final String getControlNewText()

Gets the complete new text which will be used on the control after this change. Note that some controls (such as TextField) may do further filtering after the change is made (such as stripping out newlines) such that you cannot assume that the newText will be exactly the same as what is finally set as the content on the control, however it is correct to assume that this is the case for the purpose of computing the new caret position and new anchor position (as those values supplied will be modified as necessary after the control has stripped any additional characters that the control might strip).

Returns:String

The controls proposed new text at the time of this call, according to the state set for start, end, and text properties on this Change object.

getControlTextback to summary
public final String getControlText()

This is the full text that control has before the change. To get the text after this change, use getControlNewText().

Returns:String

the previous text of control

getRangeEndback to summary
public final int getRangeEnd()

Gets the end index into the TextInputControl#getText() for the modification. This will always be a value > getRangeStart() and <= TextInputControl#getLength().

Returns:int

The end index

getRangeStartback to summary
public final int getRangeStart()

Gets the start index into the TextInputControl#getText() for the modification. This will always be a value > 0 and <= TextInputControl#getLength().

Returns:int

The start index

getSelectionback to summary
public final IndexRange getSelection()

Gets the selection of this change. Note that the selection range refers to getControlNewText(), not the current control text.

Returns:IndexRange

The selected range of this change.

getTextback to summary
public final String getText()

Gets the text used in this change. For example, this may be new text being added, or text which is replacing all the control's text within the range of start and end. Typically it is an empty string only for cases where the range is being deleted.

Returns:String

The text involved in this change. This will never be null.

isAddedback to summary
public final boolean isAdded()

Gets whether this change was in response to text being added. Note that after the Change object is modified by the formatter (by one of the setters) the return value of this method is not altered. It answers as to whether this change was fired as a result of text being added, not whether text will end up being added in the end.

Returns:boolean

true if text was being added

isContentChangeback to summary
public final boolean isContentChange()

The content change is any of add, delete or replace changes. Basically it's a shortcut for c.isAdded() || c.isDeleted() ;

Returns:boolean

true if the content changed

isDeletedback to summary
public final boolean isDeleted()

Gets whether this change was in response to text being deleted. Note that after the Change object is modified by the formatter (by one of the setters) the return value of this method is not altered. It answers as to whether this change was fired as a result of text being deleted, not whether text will end up being deleted in the end.

Returns:boolean

true if text was being deleted

isReplacedback to summary
public final boolean isReplaced()

Gets whether this change was in response to text being replaced. Note that after the Change object is modified by the formatter (by one of the setters) the return value of this method is not altered. It answers as to whether this change was fired as a result of text being replaced, not whether text will end up being replaced in the end.

Returns:boolean

true if text was being replaced

selectRangeback to summary
public final void selectRange(int newAnchor, int newCaretPosition)

Sets the selection. The anchor and caret position values must be > 0 and <= getControlNewText().getLength()}. Note that there is an order dependence here, in that the positions should be specified after the new text has been specified.

Parameters
newAnchor:int

The new anchor position

newCaretPosition:int

The new caret position

setAnchorback to summary
public final void setAnchor(int newAnchor)

Sets the anchor. The anchor value must be > 0 and <= getControlNewText().getLength()}. Note that there is an order dependence here, in that the position should be specified after the new text has been specified.

Parameters
newAnchor:int

The new anchor position

setCaretPositionback to summary
public final void setCaretPosition(int newCaretPosition)

Sets the caret position. The caret position value must be > 0 and <= getControlNewText().getLength()}. Note that there is an order dependence here, in that the position should be specified after the new text has been specified.

Parameters
newCaretPosition:int

The new caret position

setRangeback to summary
public final void setRange(int start, int end)

A method assigning both the start and end values together, in such a way as to ensure they are valid with respect to each other. The start must be less than or equal to the end.

Parameters
start:int

The new start value. Must be a valid start value

end:int

The new end value. Must be a valid end value

setTextback to summary
public final void setText(String value)

Sets the text to use in this change. This is used to replace the range from start to end, if such a range exists, or to insert text at the position represented by start == end.

Parameters
value:String

The text. This cannot be null.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override