Adding CSS File to Stylesheets in Javafx

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;
}

How to use external css file to style a javafx application

Please go through the following solution. Let me know, if you still face issues :

https://stackoverflow.com/a/22048338/1759128

How to load css file in javafx8

You need an URL and call toExternalForm in order to load a css file into your project.

Use the ClassLoader for that:

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

Is it possible to add stylesheet to JavaFX Scene from remote css file?

I think it is reasonable that JavaFX users would think it possible to do this. The documentation does specify that the add method takes a URL as a string.

Beginning with JavaFX 2.1, the Parent class has a stylesheets
property, allowing style sheets to be set on a container. This allows
for one branch of of the scene graph to have a distinct set of styles.
Any instance of Parent can have a style sheets. A child will take its
styles from its own inline styles, the style sheets of all its
ancestors, and any style sheets from the Scene.

A style sheet URL may be an absolute URL or a relative URL. If a
relative URL is given, it is resolved against the base URL of the
ClassLoader of the concrete Application class. If, for example, there
is a main class com.wicked.cool.ui.Main that extends Application, the
relative URL "com/wicked/cool/resources/styles.css" would resolve
correctly. The relative URL "../resources/styles.css" would not since
the path ".." relative to the root is not a valid path. It is often
easier to use the ClassLoader of some class to find the resource. For
example, if the "styles.css" file resides in the same package as Main,
the following code will give the correct URL:
com.wicked.cool.ui.Main.class.getResource("styles.css").toExternalForm()

Note that, beginning with JavaFX 2.1, a URL consisting of only an
absolute path (having no scheme or authority) is resolved relative to
the base URL of ClassLoader of the class that extends Application. In
other words, "/com/wicked/cool/resources/styles.css" is treated as
"com/wicked/cool/resources/styles.css". This is consistent with FXML.
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.

The documentation talks about absolute and relative URLs in relationship to the class loader though. So, if using a URL that points to some resource on the internet does not work I think the best thing you could do is file a bug report. Either a URL pointing to a resource on the internet should work, or the documentation should explicitly specify that only local resources (local to the classloader) are valid.



Related Topics



Leave a reply



Submit