Reputation: 10802
I have an extremely trivial case. I have a button and I want to add an icon to this button. The file structure of the project looks like this:
testpackage/
- Test.java
- add.gif
- my.css
So, this code works relatively fine:
Button b1 = new Button();
b1.setStyle("-fx-background-image: url('/testpackage/add.gif');" +
"-fx-background-repeat: no-repeat;" +
"-fx-background-position: center;");
But I do not want to hardcode css style, instead I want to use my.css
. This file - my.css
- looks like:
.add-button {
-fx-background-image: url('/testpackage/add.gif');
-fx-background-repeat: no-repeat;
-fx-background-position: center;
}
However, when I try to use it like so
Button b1 = new Button();
b1.getStyleClass().add("add-button");
it does not work. Thanks fro help in advance!
Upvotes: 0
Views: 112
Reputation: 2598
You should add the style sheet as well .
b1.getStylesheets().add(this.getClass().getResource("my.css").toExternalForm();
b1.getStyleClass().add("add-button");
Upvotes: 1
Reputation: 280
You have to add the stylesheet to your scene first:
Scene scene = new Scene(new Group(), 500, 400);
scene.getStylesheets().add("path/stylesheet.css");
Then you can assign classes, like you did above.
Upvotes: 1