Directly highlight one text range in RichTextFX

1.2k views Asked by At

I'm currently trying to display the contents of a file in RichTextFX and then highlight a specififc character range on a line with a red background do indicate a problem with this lines.

My code displays everything neatly, but unfortunately I get no highlighting.

code:

    InlineCssTextArea textArea = new InlineCssTextArea();

    textArea.setParagraphGraphicFactory(LineNumberFactory.get(textArea));
    textArea.setMinHeight(200.0);

    textArea.getStylesheets().add(getClass().getResource("parser.css").toExternalForm());
    try {
        List<String> yourFileLines = Files.readAllLines(file.toPath());
        textArea.replaceText(yourFileLines.stream().collect(Collectors.joining("\n")));
    } catch (IOException e) {
        e.printStackTrace();
    }
    textArea.setStyle(0, 0, 10, "error");
    textArea.setEditable(false);

parser.css:

.error {
    -rtfx-background-color: red;
}
1

There are 1 answers

0
Pagbo On BEST ANSWER

As per documentation the InlineCssTextArea#setStyle take directly the css property in parameter.

Thus, in your case it would be textArea.setStyle(0, 0, 10, "-rtfx-background-color: red;");.


Note that if you want many component with the same styles, the styleclass name is cleaner, and the best aproach (actually to my mind it is almost always the best approach). And by reading this, if you want use class name you should choose the StyleClassedTextArea instead of the InlineCssTextArea. Indeed StyleClassedTextArea accepts StyleClass as parameter of its method setStyle. (see the example below).