0

I am building a notepad project in JavaFX. For now, I have added a find and replace feature. I will type a word in a textbox, and the replaceable word in another textbox, then I replace them. Till here, Works fine. But I want to add a feature that if I write a word in search box, the word in the notepad's textarea will be highlighted. I am using JavaFX. I have searched a lot about how to add a highlighter. But most of them use java swing. For JavaFX I have no clue how to add highlighter.

public void find_and_highlight(ActionEvent e)
{
    String text=A1.getText();
    String findtext=find.getText();
    int text_ln=A1.getText().length();
    int word_ln=find.getText().length();
    for(int i=0;i<text_ln-word_ln;i++)
    {
        String sub=text.substring(i,i+word_ln);
        if(sub.equals(findtext))
        {
            // what to do here
        }
    }

} 

So in the above code, The function is activated when I write some text in " find text" string and press the button. String "text" contains the text in the textarea. If the search word is found, I want to highlight the text from index i to index i+ word_ln. So Basically I want ,

highlight(i, i + word_len);

Hope I will get help :)

Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36

1 Answers1

0

I think there is no direct way of doing that but you can try JFXHighlighter from JFoenix. Here is an example:

JFXHighlighter highlighter = new JFXHighlighter();
highlighter.setPaint(Color.YELLOW);

TextArea textArea = new TextArea("This is my text");
Button highlightButton = new Button("Highlight");
VBox parent = new VBox(textArea, highlightButton);

highlightButton.setOnAction(event -> {
    highlighter.highlight(parent, "text");
    event.consume();
});

Output:

enter image description here

Note: You need to redo highlighting when the text of the TextArea changes. You can do that simply by assigning a change listener on textProperty().

Maven dependency for Java 8:

<!-- https://mvnrepository.com/artifact/com.jfoenix/jfoenix -->
<dependency>
    <groupId>com.jfoenix</groupId>
    <artifactId>jfoenix</artifactId>
    <version>8.0.9</version>
</dependency>

Maven dependency for Java 9+:

<!-- https://mvnrepository.com/artifact/com.jfoenix/jfoenix -->
<dependency>
    <groupId>com.jfoenix</groupId>
    <artifactId>jfoenix</artifactId>
    <version>9.0.9</version>
</dependency>
Miss Chanandler Bong
  • 4,081
  • 10
  • 26
  • 36
  • Would you please explain why JFXHighlighter is not supporting in my javafx file ? Actually I am quite new in javafx, so Dont know much what is maven dependency. –  May 12 '20 at 17:48
  • @MD.TOKITAHMID Please read [this question](https://stackoverflow.com/questions/13335351/what-does-maven-do-in-theory-and-in-practice-when-is-it-worth-to-use-it) about Maven. If you're not using Maven or any other build management tool, then you can download the jar file and add to the classpath. – Miss Chanandler Bong May 13 '20 at 15:45
  • ok, so you mean If i simply download the jfeonix jar and add it to my buildpath, that will work , right? –  May 14 '20 at 07:35