Class TextFormatter.Change

java.lang.Object
javafx.scene.control.TextFormatter.Change
All Implemented Interfaces:
Cloneable
Enclosing class:
TextFormatter<V>

public static final class TextFormatter.Change extends Object implements 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:

  • Some text was deleted: In this case, text is empty and range denotes the range of deleted text. E.g. In text "Lorem ipsum dolor sit amet", removal of the second word would result in range being (6,11) and an empty text. Similarly, if you want to delete some different or additional text, just set the range. If you want to remove first word instead of the second, just call setRange(0,5)
  • Some text was added: Now the range is empty (means nothing was deleted), but it's value is still important. Both the start and end of the range point to the index wheret the new text was added. E.g. adding "ipsum " to "Lorem dolor sit amet" would result in a change with range of (6,6) and text containing the String "ipsum ".
  • Some text was replaced: The combination of the 2 cases above. Both text and range are not empty. The text in range is deleted and replaced by text in the Change. The new text is added instead of the old text, which is at the beginning of the range. 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