Closed impactCn closed 7 months ago
I solved it. Just selectRange(0, 0).
public class Find extends Application {
private final static List<SelectionImpl<Collection<String>, String, Collection<String>>> SELECTIONS = new ArrayList<>();
private CodeArea area = new CodeArea();
private TextField findInput = new TextField();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
area.replaceText(0, 0, "Your code goes here. \n Your code goes here.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nn\n\n\n\n\n\n\n\n\n\n\n\n");
findInput.setBackground(new Background(new javafx.scene.layout.BackgroundFill(Color.LIGHTGRAY, null, null)));
area.setBackground(new Background(new javafx.scene.layout.BackgroundFill(Color.WHITE, null, null)));
findInput.setPromptText("Find");
VBox vBox = new VBox(findInput, area);
primaryStage.setTitle("Find");
primaryStage.setScene(new Scene(vBox, 400, 400));
primaryStage.show();
findInput.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
for (SelectionImpl<Collection<String>, String, Collection<String>> selection : SELECTIONS) {
selection.selectRange(0, 0);
area.removeSelection(selection);
}
SELECTIONS.clear();
Pattern keyPattern = Pattern.compile(t1, Pattern.MULTILINE);
Matcher matcher = keyPattern.matcher(area.getText());
while (matcher.find()) {
addExtraSelection(matcher.start(), matcher.end());
}
}
});
}
private void addExtraSelection(int start, int end) {
SelectionImpl<Collection<String>, String, Collection<String>> extraSelection = new SelectionImpl<>(String.valueOf(start), area,
path -> {
// make rendered selection path look like a yellow highlighter
path.setStrokeWidth(0);
path.setFill(Color.YELLOW);
}
);
if (!area.addSelection(extraSelection)) {
throw new IllegalStateException("selection was not added to area");
}
// select something so it is visible
extraSelection.selectRange(start, end);
SELECTIONS.add(extraSelection);
}
}
First, Happy new year.
When I make a text search highlight function, I find that the selection is delayed.
demo
Only when you slide to a new place will the highlight disappear.