Skip to content

Instantly share code, notes, and snippets.

@TomasMikula
Created May 13, 2014 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomasMikula/70f085c36c21ce964f07 to your computer and use it in GitHub Desktop.
Save TomasMikula/70f085c36c21ce964f07 to your computer and use it in GitHub Desktop.
Demonstration of TextArea's observable state inconsistency
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.IndexRange;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class TextAreaConsistency extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TextArea area = new TextArea();
area.caretPositionProperty().addListener(obs -> checkConsistency(area));
area.anchorProperty().addListener(obs -> checkConsistency(area));
area.selectionProperty().addListener(obs -> checkConsistency(area));
stage.setScene(new Scene(area));
stage.show();
}
private static void checkConsistency(TextArea area) {
IndexRange selection = area.getSelection();
int caretPosition = area.getCaretPosition();
int anchor = area.getAnchor();
assert selection.equals(IndexRange.normalize(anchor, caretPosition));
}
}
@TomasMikula
Copy link
Author

Run the above program and type a letter into the text area. Don't forget to enable assertions. Tested with JDK 8u20 b13.

@TomasMikula
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment