Class TextFormatter.Change
- All Implemented Interfaces:
Cloneable
- Enclosing class:
TextFormatter<V>
public static final class TextFormatter.Change extends Object implements Cloneable
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:
-
Some text was deleted: In this case,
textis empty andrangedenotes therangeof deleted text. E.g. In text "Lorem ipsum dolor sit amet", removal of the second word would result inrangebeing (6,11) and an emptytext. Similarly, if you want to delete some different or additional text, just set therange. If you want to remove first word instead of the second, just callsetRange(0,5) -
Some text was added: Now the
rangeis empty (means nothing was deleted), but it's value is still important. Both the start and end of therangepoint to the index wheret the new text was added. E.g. adding "ipsum " to "Lorem dolor sit amet" would result in a change withrangeof (6,6) andtextcontaining the String "ipsum ". -
Some text was replaced: The combination of the 2 cases above. Both
textandrangeare not empty. The text inrangeis deleted and replaced bytextin the Change. The new text is added instead of the old text, which is at the beginning of therange. E.g. when some text is being deleted, you can simply replace it by some placeholder text just by setting a new text (setText("new text"))
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
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
TextFormatter.Change |
clone() |
|
final int |
getAnchor() |
Gets the new anchor. |
final int |
getCaretPosition() |
Gets the new caret position. |
final Control |
getControl() |
Gets the control associated with this change. |
final int |
getControlAnchor() |
Gets the current anchor position of the control. |
final int |
getControlCaretPosition() |
Gets the current caret position of the control. |
final String |
getControlNewText() |
Gets the complete new text which will be used on the control after this change. |
final String |
getControlText() |
This is the full text that control has before the change. |
final int |
getRangeEnd() |
Gets the end index into the TextInputControl.getText() for the modification. |
final int |
getRangeStart() |
Gets the start index into the TextInputControl.getText() for the modification. |
final IndexRange |
getSelection() |
Gets the selection of this change. |
final String |
getText() |
Gets the text used in this change. |
final boolean |
isAdded() |
Gets whether this change was in response to text being added. |
final boolean |
isContentChange() |
The content change is any of add, delete or replace changes. |
final boolean |
isDeleted() |
Gets whether this change was in response to text being deleted. |
final boolean |
isReplaced() |
Gets whether this change was in response to text being replaced. |
final void |
selectRange |
Sets the selection. |
final void |
setAnchor |
Sets the anchor. |
final void |
setCaretPosition |
Sets the caret position. |
final void |
setRange |
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. |
final void |
setText |
Sets the text to use in this change. |
Method Details
getControl
public final Control getControl()
- Returns:
- The control associated with this change. This will never be null.
getRangeStart
public final int getRangeStart()
TextInputControl.getText() for the modification. This will always be a value > 0 and <= TextInputControl.getLength().- Returns:
- The start index
getRangeEnd
public final int getRangeEnd()
TextInputControl.getText() for the modification. This will always be a value > getRangeStart() and <= TextInputControl.getLength().- Returns:
- The end index
setRange
public final void setRange(int start, int end)
- Parameters:
-
start- The new start value. Must be a valid start value -
end- The new end value. Must be a valid end value
getCaretPosition
public final int getCaretPosition()
getControlNewText().getLength()}- Returns:
- The new caret position
getAnchor
public final int getAnchor()
getControlNewText().getLength()}- Returns:
- The new anchor position
getControlCaretPosition
public final int getControlCaretPosition()
- Returns:
- The previous caret position
getControlAnchor
public final int getControlAnchor()
- Returns:
- The previous anchor
selectRange
public final void selectRange(int newAnchor, int newCaretPosition)
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- The new anchor position -
newCaretPosition- The new caret position
getSelection
public final IndexRange getSelection()
getControlNewText(), not the current control text.- Returns:
- The selected range of this change.
setAnchor
public final void setAnchor(int newAnchor)
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- The new anchor position
setCaretPosition
public final void setCaretPosition(int newCaretPosition)
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- The new caret position
getText
public final String getText()
- Returns:
- The text involved in this change. This will never be null.
setText
public final void setText(String value)
- Parameters:
-
value- The text. This cannot be null.
getControlText
public final String getControlText()
getControlNewText().- Returns:
- the previous text of control
getControlNewText
public final String getControlNewText()
- 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.
isAdded
public final boolean isAdded()
- Returns:
- true if text was being added
isDeleted
public final boolean isDeleted()
- Returns:
- true if text was being deleted
isReplaced
public final boolean isReplaced()
- Returns:
- true if text was being replaced
isContentChange
public final boolean isContentChange()
c.isAdded() || c.isDeleted() ;- Returns:
- true if the content changed
clone
public TextFormatter.Change clone()
- Overrides:
-
clonein classObject
© 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.