0

I am trying to highlight a word in JavaFX that is found by Matcher and whenever new one is one I want to highlighted that new one. Is this possible? Here is my code:

                String s1 = displayArea.getText();
                String s2 = textInputArea.getText();
                Pattern p = Pattern.compile("[a-zA-Z]*[^\\s]");            
                Matcher m1 = p.matcher(s1);
                Matcher m2 = p.matcher(s2);
                int counter=0;


                System.out.println("Words from string \"" + s1 + "\" : ");
                while (m1.find() && m2.find() ) {

                    System.out.println("Found words: "+m1.group()+ " Typed words: "+m2.group() );
                    //m1.group() needs to be highlighted and displayed in textarea
                    if (m1.group().equals(m2.group())){
                        counter++;
                        System.out.println("counter:"+counter );

                    }else if (!(m1.group().equals(m2.group())) && typedKey==' '){
                            negativni++;
                            System.out.println("negativni:"+ negativni );

                    }

                }
koweratus
  • 23
  • 5
  • Do you have a TextArea where you want to highlight the words? If yes, you may want to have a look at this: https://stackoverflow.com/questions/9128535/highlighting-strings-in-javafx-textarea – anko May 28 '20 at 17:38
  • That entirely depends on what you mean by "highlight" and how you display the string. I don't understand the second half of the first sentence though. Not sure why you what you're trying to do with the loop though. You're iterating through both strings in parallel. Also not sure what `typedKey` is; btw `m1.group().equals(m2.group())` yields `false`, you don't need to check again, if it did indeed yield `false`. Since the result of `group()` does not change, you should already know about the result... – fabian May 28 '20 at 18:27

1 Answers1

1

I solved it using RichTextFX.

displayArea.setStyle(m1.start(),m1.end(),-fx-font-weight:bold;");

koweratus
  • 23
  • 5