Maybe a TitledPane bug

Sverre Moe sverre.moe at gmail.com
Thu Nov 1 18:19:15 UTC 2018


I am now not so sure it is a bug. Seems the problem is the hover pseudo
state that resets the background color.
>From modena.css
.titled-pane > .title:hover {
    -fx-color: -fx-hover-base;
}

Try this example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class TitledPaneApplication extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        final StackPane root = new StackPane();

        final TitledPane titledPane = new TitledPane();
        titledPane.setText("Title");
        titledPane.setAnimated(false);
        titledPane.setCollapsible(false);
        root.getChildren().add(titledPane);

        final ToggleButton button = new ToggleButton("Change");
        button.setOnAction(event -> {
            boolean selected = button.isSelected();
            final Region titleNode = (Region) titledPane.lookup(".title");
            if (selected) {
                titleNode.setBackground(new Background(new
BackgroundFill(Color.GREEN, null, null)));
            } else {
                titleNode.setBackground(null);
            }
        });

        button.setSelected(false);
        titledPane.setContent(button);

        final Scene scene = new Scene(root, 400, 400);

scene.getStylesheets().add(getClass().getResource("light.css").toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.setTitle("TestApplication");
        primaryStage.show();
    }

}

Here is the CSS content for light.css
.titled-pane > .title {
    -fx-color: rgb(220, 220, 220);
}

/Sverre


Den tor. 1. nov. 2018 kl. 14:45 skrev David Grieve <david.grieve at oracle.com
>:

> 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)));
> }
>
>
>
> On 10/31/18 6:58 PM, Sverre Moe wrote:
> > 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
>
>


More information about the openjfx-dev mailing list