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 :)