Discussion:
Maybe a TitledPane bug
Sverre Moe
2018-10-31 22:58:12 UTC
Permalink
I am not sure if I have stumbled upon a bug in JavaFX, or perhaps it is
simply something I am doing wrong.

After setting the TitledPane title background programmatically, then when
hovering the background color is reset to the previous value from CSS.
I get same behavior in both JavaFX 8 and JavaFX 11.

Color color = Color.valueOf("#00ff11");
titleNode.setBackground(new Background(new BackgroundFill(color, null,
null)));

titleNode.setStyle("-fx-background-color:#00ff11;");

Setting the style property seems to work, but why doesn't the
programmatically approach work?

https://stackoverflow.com/questions/53083005/javafx-titledpane-changed-title-background-is-reset-on-mouse-entered/

/Sverre
David Grieve
2018-11-01 13:45:13 UTC
Permalink
It's hard to tell without some short, self-contained, correct example.
This sample I crafted here doesn't reproduce the issue.

@Override public void start(Stage primaryStage) {

final TitledPane titledPane =new TitledPane("Button Pane",new Button("Button"));
final HBox root =new HBox();
root.getChildren().add(titledPane);

primaryStage.setTitle("Maybe a TitledPane bug");
primaryStage.setScene(new Scene(root,300,250));
primaryStage.show();

final Region titleNode = (Region)titledPane.lookup("*.title");
titleNode.setBackground(new Background(new BackgroundFill(Color.GREEN,null,null)));
}
Post by Sverre Moe
I am not sure if I have stumbled upon a bug in JavaFX, or perhaps it is
simply something I am doing wrong.
After setting the TitledPane title background programmatically, then when
hovering the background color is reset to the previous value from CSS.
I get same behavior in both JavaFX 8 and JavaFX 11.
Color color = Color.valueOf("#00ff11");
titleNode.setBackground(new Background(new BackgroundFill(color, null,
null)));
titleNode.setStyle("-fx-background-color:#00ff11;");
Setting the style property seems to work, but why doesn't the
programmatically approach work?
https://stackoverflow.com/questions/53083005/javafx-titledpane-changed-title-background-is-reset-on-mouse-entered/
/Sverre
Sverre Moe
2018-11-01 18:19:15 UTC
Permalink
This post might be inappropriate. Click to display it.
David Grieve
2018-11-01 18:49:56 UTC
Permalink
Post by Sverre Moe
scene.getStylesheets().add(getClass().getResource("light.css").toExternalForm());
Here is the CSS content for light.css
.titled-pane > .title {
    -fx-color: rgb(220, 220, 220);
Well, there you go. Your 'light.css' style trumps the setBackground. The
reason for this is that this allows an application to be deployed with
explicitly set, styleable properties - e.g. titleNode.setBackground(...)
- but still allow that application to be styled from a stylesheet.

From the JavaFX CSS reference:

The implementation allows designers to style an application by using style
sheets to override property values set from code. This has implications for
the cascade; particularly, when does a style from a style sheet override a
value set from code? The JavaFX CSS implementation applies the following
order of precedence; a style from a user agent style sheet has lower
priority than a value set from code, which has lower priority than a Scene
or Parent style sheet. Inline styles have highest precedence. Style sheets
from a Parent instance are considered to be more specific than those styles
from Scene style sheets.

Loading...