Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add CSS stylesheets in JavaFX

Tags:

java

css

javafx

I would like to add a CSS file which is located somewhere on the filesystem. The purpose is to write an application where the user can add JavaFX CSS files (which are created by anyone and located anywhere) dynamically.
I tried something like that, only for testing, to see if dynamically added CSS files works:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Label label = new Label("Hello");
        Scene scene = new Scene(label);

        //file would be set by an file chosser
        File file = new File("C:/test.css");
        scene.getStylesheets().add(file.getAbsolutePath());

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


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

But I get always the same error:

WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "C:\test.css" not found. 

How can I fix it?

like image 202
Species Avatar asked Apr 26 '13 12:04

Species


4 Answers

If css in same packaage simply use

scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
like image 180
jijesh Aj Avatar answered Nov 20 '22 06:11

jijesh Aj


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;
}
like image 41
Antonio J. Avatar answered Nov 20 '22 04:11

Antonio J.


You can get the URL from java.io.File

File file = new File("style.css");
URL url = file.toURI().toURL();
scene.getStylesheets().add(url.toExternalForm());

or in short form

scene.getStylesheets().add((new File("style.css")).toURI().toURL().toExternalForm());
like image 4
HW90 Avatar answered Nov 20 '22 04:11

HW90


The exception is thrown because the string "C:/test.css" is not a URI resource. Therefore you must convert your string into a URI resource.

As of Java 7 you can do:

String uri = Paths.get("C:/test.css").toUri().toString();
scene.getStylesheets().add(uri);
like image 1
pgmank Avatar answered Nov 20 '22 05:11

pgmank