1

I use a toggleSwitch in JavaFX from fxControls. I can override by a CSS file this property:

.toggle-switch:selected .thumb-area {
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -20%), derive(-fx-text-box-border, -30%)), linear-gradient(to bottom, derive(#0b99c9, 30%), #0b99c9);
    -fx-background-insets: 0, 1;
}

But I would like to do it by code to change some colors in the component (for example the blue background in selected mode) but This doesn't work:

String css = ".toggle-switch:selected .thumb-area{-fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -20%), derive(-fx-text-box-border, -30%)),linear-gradient(to bottom, derive(#0b99c9, 30%), #0b99c9);-fx-background-insets: 0, 1;}";
toggleSwitch.setStyle(css);

Thanks for your help.

4
  • Can you provide a use-case as to why you want to do this in code? It's possible, but ugly, and it's almost certainly possible and cleaner using CSS. If you programmatically want to change the style when the toggle is selected/unselected, you need to register a listener with the selectedProperty() and update the style accordingly for both cases. Furthermore, to access the .thumb-area would require a CSS lookup. Note that the setStyle(...) method is expecting only a style rule, not a selector. Commented Mar 10, 2020 at 16:02
  • The code is integrated in a legacy code that have a constant for a color that is used in lot of places in the code (and for some Swing code too), the idea is to reuse this constant instead of css file to have only one place to change the color. Commented Mar 10, 2020 at 16:12
  • Use a looked-up color. Edit your question to explain what you actually need, and I'll post a much better solution than hacking around with setStyle() on lots of different UI elements. Commented Mar 10, 2020 at 16:19
  • Ok I mad it clearer that it's for color customisation (the blue background in my use case), thanks for your help. Commented Mar 10, 2020 at 16:31

1 Answer 1

1

I think you can do what you're trying to achieve using a looked-up color. In your CSS file, do:

.root {
  -my-selected-control-color: #0b99c9 ;
}

And then define all your styles in terms of the looked-up color you define:

.toggle-switch:selected .thumb-area{
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -20%), derive(-fx-text-box-border, -30%)),
                            linear-gradient(to bottom, derive(-my-selected-control-color, 30%), -my-selected-control-color);
    -fx-background-insets: 0, 1;

}

You can define as many of these as you need.

Now you only have a single place to change the color (in the CSS file) if you need to. You can also modify this programmatically with a single line of code:

// assume root is the root of the UI
// you can always retrieve this with Node root = anyNode.getScene().getRoot();
// This will change all occurrences of -my-selected-control-color to 
// a bright green:
root.setStyle("-my-selected-control-color: #00b140 ;");

Also see this related post.

Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm! Thanks a lot
@PhilippePeter Yes; it's a pretty neat feature. I haven't tried the following for a while, and it's completely undocumented (to the extent that it might not have even been intentional, so it's not necessarily guaranteed to work), but it seems to work with values other than colors (sizes, etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.