-
-
Save TomasMikula/70f085c36c21ce964f07 to your computer and use it in GitHub Desktop.
Demonstration of TextArea's observable state inconsistency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run the above program and type a letter into the text area. Don't forget to enable assertions. Tested with JDK 8u20 b13.