Reputation: 183
I have to code a full text search. If the word is found it's highlighted and if I press the 'Next' button the next word in the text is highlighted and so on. But the challange is, that the words further down in the text have to be highlighted in the middle of the window. Means, the text has to scroll automatically to the highlighted word. I read a few posts on Stackoverflow but I couldn't get it to work.
This is my find methode:
findButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
errorText.setText("");
if (textFieldFind.getText() != null) {
findWord = Pattern.compile(textFieldFind.getText(), Pattern.CASE_INSENSITIVE);
matcher = findWord.matcher(textArea.getText());
if (matchCase.isSelected()) {
findWord = Pattern.compile(textFieldFind.getText());
matcher = findWord.matcher(textArea.getText());
}
if (matcher.find()) {
textArea.selectRange(matcher.start(), matcher.end());
textArea.scr
}
} else {
errorText.setText("Missing search key");
}
}
});
This is my 'Next' button methode:
nextButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (matcher.find()) {
scroll.setVvalue(0.5);
textArea.selectRange(matcher.start(), matcher.end());
}
}
});
Upvotes: 1
Views: 340
Reputation: 13859
Try something like this:
Platform.runLater(()->textArea.setScrollTop(matcher.start()));
This worked for me
Upvotes: 1