Dear Pavel:

Can you clarify what you are trying to do exactly?

If you are trying to add a background to a text segment within the paragraph 
(as your code seem to indicate), then the only way to do it is to call 
Builder.addHighlight().

If you are trying to set the background of the whole paragraph, you've hit 
another missing API similar to JDK-8355774 - there is currently no way to style 
the paragraphs via CSS.  There is the 
Builder.setParagraphAttributes(StyleAttributeMap) but a CSS one is missing.

-andy




From: openjfx-dev <openjfx-dev-r...@openjdk.org> on behalf of PavelTurk 
<pavelturk2...@gmail.com>
Date: Saturday, May 3, 2025 at 10:07
To: openjfx-dev@openjdk.org <openjfx-dev@openjdk.org>
Subject: CodeArea: -fx-background-color doesn't work.
For styling CodeArea, I use exclusively style classes, and this is the 
foundation of my entire architecture.
Today I tried to implement search highlighting (via background color), but it 
didn't work. Below is my test code.

Can anyone tell me how to set the background color using CSS? For example, in 
RichTextFX's CodeArea,
they have -rtfx-background-color.

public class JfxCodeArea extends Application {

     @Override
     public void start(Stage primaryStage) throws Exception {
         String text = """
                       Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
sed do eiusmod tempor incididunt
                       ut labore et dolore magna aliqua. Ut enim ad minim 
veniam, quis nostrud exercitation ullamco
                       laboris nisi ut aliquip ex ea commodo consequat. Duis 
aute irure dolor in reprehenderit
                       in voluptate velit esse cillum dolore eu fugiat nulla 
pariatur.
                       """;

         String css = """
             .test {
                 -fx-font-weight: bold;
                 -fx-fill: red;
                 -fx-background-color: green;
             }

         """;
         String data = "data:text/css;base64," + 
Base64.getEncoder().encodeToString(css.getBytes(StandardCharsets.UTF_8));

         CodeArea codeArea = new CodeArea();
         codeArea.getStylesheets().add(data);

         codeArea.setSyntaxDecorator(new SyntaxDecorator() {
             @Override
             public RichParagraph createRichParagraph(CodeTextModel model, int 
index) {
                 var builder = RichParagraph.builder();
                 builder.addWithStyleNames(model.getPlainText(index), "test");
                 return builder.build();
             }

             @Override
             public void handleChange(CodeTextModel m, TextPos start, TextPos 
end, int charsTop, int linesAdded, int charsBottom) {

             }
         });

         VBox.setVgrow(codeArea, Priority.ALWAYS);
         var button = new Button("Go!");
         button.setOnAction(e -> codeArea.setText(text));
         VBox root = new VBox(codeArea, button);
         Scene scene = new Scene(root, 600, 200);
         primaryStage.setScene(scene);
         primaryStage.show();

     }

     public static void main(String[] args) {
         launch(args);
     }
}

Best regards, Pavel

Reply via email to