Javafx Change CSS at Runtime

Dynamically change JavaFX css property

The method getStyleClass() returns a list of CSS class names associated with the node. These class names are used to determine which rules in an external CSS file are applied to the node. You cannot pass CSS selectors and rules in here.

Instead, write an external CSS file which contains a rule for the background color for the track. You can use a "looked-up color" here, which basically works like a "CSS variable". Define the "looked-up color" for the slider, and use it in a rule for the track to set the background color:

style.css:

.slider {
-track-color: white ;
}
.slider .track {
-fx-background-color: -track-color ;
}

Now, in the Java code, you can update the value for the looked-up color by calling setStyle("-track-color: /* someColor */") on the slider. Here is a quick example:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SliderTest extends Application {

@Override
public void start(Stage primaryStage) {
Slider slider = new Slider();
slider.valueProperty().addListener((obs, oldValue, newValue) -> {
double percentage = 100.0 * (newValue.doubleValue() - slider.getMin()) / (slider.getMax() - slider.getMin());
slider.setStyle("-track-color: linear-gradient(to right, #90C7E0 " + percentage+"%, white "+percentage+("%);"));
});
StackPane root = new StackPane(slider);
root.setPadding(new Insets(20));
Scene scene = new Scene(root);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}

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

In your application, you can do the same using the current time of the media player (or just register a listener on the slider's value property, as that is changing too).

JavaFX with FXML change css file runtime, with menu-items

I've found it :

To use predefinite styles (like Modena and Caspian) :

Application.setUserAgentStylesheet(STYLESHEET_MODENA);

To use your own css stylesheet :

Application.setUserAgentStylesheet(null);
StyleManager.getInstance().addUserAgentStylesheet(
getClass().getResource("NewSkin.css").toExternalForm());

Dynamically add CSS stylesheets in JavaFX

Your problem is that you aren't using a URL. Here you can find more documentation on how the CSS is loaded alongside the CSS reference.

If you have the URL as a String you could set the CSS dynamically with an external file like so:

private boolean isANext = true;

public void start(Stage primaryStage) throws Exception {
Button button = new Button("Change CSS");
VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().add(button);
scene = new Scene(vbox, 200, 200);

button.setOnAction(ev -> {
// Alternate two stylesheets just for this demo.
String css = isANext ? "file:///C:/temp/a.css" : "file:///C:/temp/b.css";
isANext = !isANext;
System.out.println("Loading CSS at URL " + css);

scene.getStylesheets().clear();
scene.getStylesheets().add(css);
});

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

In the a.css

.button {    
-fx-text-fill: white;
-fx-background-color: red;
}

And in b.css

.button {    
-fx-text-fill: white;
-fx-background-color: black;
}

Using controller to change css file used with radioButtons

Would the method I posted about loading a different css file work?

No.

mainS.getStyleClass().add(css1);

will not work, since getStyleClass() accepts the CSS selectors defined in CSS style sheet file, and not its path or name.

It should be

mainS.getStyleSheets().add(css1);

See this example of changing css at runtime.

JavaFX - Change CSS specific values in runtime

See the predefined colors of JavaFX from modena.css (or caspian.css for older JavaFX-2). For example. These colors are base for the most controls of JavaFX, but there are of course more specific colors defined for some controls only.

You can change them on runtime, and apply to top level (or any) pane, i.e. parent root of scene:

Color selectedColor = colorPicker.getValue();

String rgb = getRgbString( selectedColor );
myScene.getRoot().setStyle( "-fx-accent: " + rgb + "; -fx-focus-color:" + rgb );

where

private String getRgbString( Color color )
{
int r = ( int ) Math.round( color.getRed() * 255.0 );
int g = ( int ) Math.round( color.getGreen() * 255.0 );
int b = ( int ) Math.round( color.getBlue() * 255.0 );

return "rgb(" + r + "," + g + "," + b + ")";
}

You may also make the Color more brighter, darker and/or derive new color from it. Refer to api.



Related Topics



Leave a reply



Submit