JDK-8356436 : Add support for styling background color via CSS in RichTextArea
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: jfx24
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2025-05-05
  • Updated: 2025-08-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
Currently, the only way to set the background color of a text segment in RichTextArea or CodeArea is through Java code, using RichParagraph.Builder#addHighlight(int start, int length, Color color).

However, one of the key strengths of JavaFX is its support for CSS-based styling, which allows developers to style components without writing code. Among the most fundamental aspects of text styling are foreground and background colors, both of which have been supported since CSS1 (1996).

Therefore, I propose adding support for styling background color of text via CSS. This is an essential and widely expected feature.



Comments
Additional Information From Submitter: ====================================== After reviewing both issues — JDK-8355774 and JDK-8356436 — I have concluded that JDK-8355774 should be closed in favor of pursuing JDK-8356436. Introducing separate methods for different stylistic attributes is, in my view, a misguided and potentially harmful direction that could lead to serious design and maintenance issues in the future. This problem should be addressed by adding the missing CSS properties, rather than expanding the API with hardcoded styling methods. This is the approach taken by the developers of RichTextFX, and it aligns far better with the design philosophy of JavaFX. The proposed method-based styling approach contradicts the fundamental principles of CSS, which promote a clear separation between styling and logic. Encoding visual effects into method names tightly couples presentation to code, undermining reusability and flexibility.
09-05-2025

So the basic idea is to add -fx-background-color styleable property to the component which visualizes a text segment in RichParagraph.
08-05-2025

There are two ways to style background color: either with paragraph attribute StyleAttributeMap.BACKGROUND, or using highlights. What you ask should be fulfilled by the highlights, except the API that allows to style the highlights with CSS is currently missing (see JDK-8355774). Once the new APIs are added (RichParagraph.Builder#addHighlight(int start, int length, String ... css), you can use the stylesheet. Implementation note: The text segments (that is, the portions of text that have the same attributes or styles) are implemented with Text object (extends Shape) which does not support background color. The highlights are implemented as separate shapes that are added to the paragraph node behind the TextFlow's children. This is done to support highlighting not only text, but also images and Nodes that might be present in the rich paragraph.
07-05-2025