【问题标题】:search a word by key enter按键输入
【发布时间】:2019-05-24 08:45:43
【问题描述】:

我的搜索方法有问题。

通过这种方法,我可以在文本字段中输入一个单词,并在文本区域中显示该单词。但是,如果我让它运行,这只发生一次。我需要展开它,这样每次我点击“enter”时,程序都应该继续在 textarea 中搜索。我怎样才能做到这一点? 请给我代码示例。我的演讲只剩下 2 天了。

非常感谢您的帮助

textfield.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent event) {
            if (event.getCode() == KeyCode.ENTER)  {
                String text = textarea.getText();

              Labeled errorText = null;
            if (textfield.getText() != null && !textfield.getText().isEmpty()) {
                    index = textarea.getText().indexOf(textfield.getText()); 
                    textarea.getText();

                    if (index == -1) {
                        errorText.setText("Search key Not in the text");
                    } else {
                      //  errorText.setText("Found");
                        textarea.selectRange(index, index + textfield.getLength());

                        }


                    }   

                }
            }

        });

【问题讨论】:

标签: javafx key enter pressed


【解决方案1】:

overloaded version of the indexOf method 允许您从特定索引开始搜索。跟踪您上次查找的索引并从该位置开始搜索:

@Override
public void start(Stage primaryStage) throws Exception {

    TextField textField = new TextField("foo");
    TextArea textarea = new TextArea();
    for (int i = 0; i < 10; i++) {
        textarea.appendText("foo\nbarfoobarfoofoo\n");
    }

    textField.setOnAction(evt -> {
        String searchText = textField.getText();
        if (searchText.isEmpty()) {
            return; // searching for empty text doesn't make sense
        }
        int index = textarea.getSelection().getEnd();

        // in case of the first search, start at the beginning
        // TODO: adjust condition/starting index according to needs
        if (textarea.getSelection().getLength() == 0) {
            index = 0;
        }

        // find next occurrence
        int newStartIndex = textarea.getText().indexOf(searchText, index);

        // mark occurrence
        if (newStartIndex >= 0) {
            textarea.selectRange(newStartIndex, newStartIndex + searchText.length());
        }
    });

    Scene scene = new Scene(new VBox(textField, textarea));
    primaryStage.setScene(scene);
    primaryStage.show();
}

编辑

如果你对选择后的元素不满意(或光标后,如果没有选择范围),你可以保存最后一次匹配结束的数据:

@Override
public void start(Stage primaryStage) throws Exception {

    TextField textField = new TextField("foo");
    TextArea textarea = new TextArea();
    for (int i = 0; i < 10; i++) {
        textarea.appendText("foo\nbarfoobarfoofoo\n");
    }

    class SearchHandler implements EventHandler<ActionEvent> {
        int index = 0;

        @Override
        public void handle(ActionEvent event) {
            String searchText = textField.getText();
            String fullText = textarea.getText();

            if (index + searchText.length() > fullText.length()) {
                // no more matches possible
                // TODO: notify user
                return;
            }
            // find next occurrence
            int newStartIndex = textarea.getText().indexOf(searchText, index);

            // mark occurrence
            if (newStartIndex >= 0) {
                index = newStartIndex + searchText.length();
                textarea.selectRange(newStartIndex, index);
            } else {
                index = fullText.length();
                // TODO: notify user
            }
        }

    }

    SearchHandler handler = new SearchHandler();
    textField.setOnAction(handler);
    // reset index to search from start when changing the text of the TextField
    textField.textProperty().addListener((o, oldValue, newValue) -> handler.index = 0);

    Scene scene = new Scene(new VBox(textField, textarea));
    primaryStage.setScene(scene);
    primaryStage.show();
}

【讨论】:

  • 如果文本或选择在调用之间发生变化似乎没有帮助 - 或者我忽略了什么?
  • @kleopatra 许多文本编辑器在选定范围之后开始搜索。 OP 程序应该如何运行......当然,您可以将有关最后一场比赛的数据保存在某处。我在答案中添加了一些代码以启发您。
猜你喜欢
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多