Issue
I need to add a background colour to a Group in JavaFX. I tried to add it with CSS, but for some reason it did not work. CSS works for all other layout managers, but not with Group. What do I do?
Solution
Solution
Replace your usage of Group
with a Pane
and things will behave similarly, except you will gain the ability to do stuff like style the pane's background using CSS.
Background
If you want to style a parent node with CSS, use something which derives from Region:
Region is the base class for all JavaFX Node-based UI Controls, and all layout containers. It is a resizable Parent node which can be styled from CSS. It can have multiple backgrounds and borders. It is designed to support as much of the CSS3 specification for backgrounds and borders as is relevant to JavaFX.
A Group is designed to be a very light-weight parent, which incurs minimum processing and storage overhead, hence it supports only very minimal CSS properties (and does not support CSS backgrounds).
Regions, on the other hand, offer extensive CSS styling capabilities.
A Pane is a concrete Region subclass which behaves most like a group (e.g. it does not do implicit layout and you manually lay out the nodes in the pane).
Alternate Solution
This alternate solution allows you to add a "background" node to a group. It works in code, not CSS.
Items which you add to a group are layered by the painting algorithm, from back to front. So add a colored rectangle as the first item in the group and the rectangle will effectively form the background for the group.
Answered By - jewelsea
Answer Checked By - Willingham (JavaFixing Volunteer)