搜索文本并自动向下滚动

时间:2016-11-09 12:46:07

标签: search javafx scroll

我必须编写全文搜索代码。如果找到该单词,则会突出显示该单词,如果我按下'下一步'按钮突出显示文本中的下一个单词,依此类推。但是,挑战是,文本中更深层次的词必须在窗口中间突出显示。意思是,文本必须自动滚动到突出显示的单词。我在Stackoverflow上阅读了一些帖子,但我无法让它工作。

这是我的发现方法:

   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");
                } 
            }
        });

这是我的&#39;下一步&#39;按钮方法:

      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());
                }
            }
        });

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

Platform.runLater(()->textArea.setScrollTop(matcher.start()));

这对我有用

相关问题