Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaFX 8 can I provide a stylesheet from a String?

Is it possible to wrap a whole Stylesheet in a string and apply it to a certain node? Usage case would be to add specific (non changeble) behavior for PseudoClass. I know I can use pane.getStylesheets().add(getClass().getResource("mycss.css").toExternalForm());, but I would like to know if there's some way to embrd it direcly in source; something along the lines:

pane.getStylesheets().add(
    ".button:ok { -fx-background-color: green; }\n"+
    ".button:ko { -fx-background-color: red; }");
like image 679
ZioByte Avatar asked Jul 11 '14 18:07

ZioByte


People also ask

Can CSS be used in JavaFX?

CSS styles are applied to nodes in the JavaFX scene graph in a way similar to the way CSS styles are applied to elements in the HTML DOM. Styles are first applied to the parent, then to its children.


1 Answers

Since JavaFX 17 it is now possible to use data URIs.

For example,

scene.getStylesheets().add("data:text/css;base64," + Base64.getEncoder().encodeToString("* { -fx-color: red; }".getBytes(StandardCharsets.UTF_8)));

will simply work in JavaFX 17.

like image 66
Nand Avatar answered Nov 16 '22 02:11

Nand