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
  • Method Details

    • getControl

      public final Control getControl()
      Gets the control associated with this change.
      Returns:
      The control associated with this change. This will never be null.
    • getRangeStart

      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:
      The start index
    • getRangeEnd

      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:
      The end index
    • setRange

      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 - 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()
      Gets the new caret position. This value will always be > 0 and <= getControlNewText().getLength()}
      Returns:
      The new caret position
    • getAnchor

      public final int getAnchor()
      Gets the new anchor. This value will always be > 0 and <= getControlNewText().getLength()}
      Returns:
      The new anchor position
    • getControlCaretPosition

      public final int getControlCaretPosition()
      Gets the current caret position of the control.
      Returns:
      The previous caret position
    • getControlAnchor

      public final int getControlAnchor()
      Gets the current anchor position of the control.
      Returns:
      The previous anchor
    • selectRange

      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 - The new anchor position
      newCaretPosition - The new caret position
    • getSelection

      public final IndexRange getSelection()
      Gets the selection of this change. Note that the selection range refers to getControlNewText(), not the current control text.
      Returns:
      The selected range of this change.
    • setAnchor

      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 - The new anchor position
    • setCaretPosition

      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 - The new caret position
    • getText

      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:
      The text involved in this change. This will never be null.
    • setText

      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 - The text. This cannot be null.
    • getControlText

      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:
      the previous text of control
    • getControlNewText

      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:
      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()
      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:
      true if text was being added
    • isDeleted

      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:
      true if text was being deleted
    • isReplaced

      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:
      true if text was being replaced
    • isContentChange

      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:
      true if the content changed
    • clone

      public TextFormatter.Change clone()
      Overrides:
      clone in class Object