Marta
Marta

Reputation: 183

Searching and highlighting in a TextArea

I have a TextArea with a text in which I want to search for word(s). The search works, but highlighting the word with selectRange() does not. Is there a different method for highlighting?

            findButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override 
            public void handle(MouseEvent e) {

                if (textField.getText() != null && !textField.getText().isEmpty()) {
                    int index = textArea.getText().indexOf(textField.getText()); 
                    if (index == -1) {
                        errorText.setText("Search key Not in the text");
                    } else {
                      //  errorText.setText("Found");
                        textArea.selectRange(textField.getText().charAt(0), textField.getLength());    
                    }       
                } else {
                    errorText.setText("Missing search key");
                  //  errorText.setFill(Color.RED);

                }
            }
        }); 

Upvotes: 0

Views: 2369

Answers (1)

James_D
James_D

Reputation: 209684

Surely you mean

textArea.selectRange(index, index + textField.getLength());

Upvotes: 2

Related Questions