CodeArea: -fx-background-color doesn't work.

PavelTurk pavelturk2000 at gmail.com
Sat May 3 17:07:00 UTC 2025


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


More information about the openjfx-dev mailing list