直接突出显示RichTextFX中的一个文本范围



我目前正在尝试在RichTextFX中显示文件的内容,然后用红色背景高亮显示一行上的指定字符范围。这表明这行有问题。

我的代码整齐地显示了所有内容,但不幸的是,我没有得到突出显示。

代码:

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;
}

根据文档,InlineCssTextArea#setStyle直接获取参数中的css属性。

因此,在您的情况下,它将是textArea.setStyle(0, 0, 10, "-rtfx-background-color: red;");


注意如果您想要多个具有相同样式的组件,样式类名会更干净,也是最好的方法(实际上在我看来,这几乎总是最好的方法(。读到这里,如果你想使用类名,你应该选择StyleClassedTextArea而不是InlineCssTextArea。实际上,StyleClassedTextArea接受StyleClass作为其方法setStyle的参数。(参见下面的示例(。

最新更新