Desktop Java

JavaFX Tip 12: Define Icons in CSS

When you are a UI developer coming from Swing like me then there is a good chance that you are still setting images / icons directly in your code. Most likely something like this:
 
 
 
 
 
 
 
 

01
02
03
04
05
06
07
08
09
10
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
 
public class MyLabel extends Label {
 
    public MyLabel() {
        setGraphic(new ImageView(MyLabel.class.
            getResource("image.gif").toExternalForm()));
    }
}

 
imageexample

In this example the image file is looked up via Class.getResource(), the URL is passed to the constructor of the ImageView node and this node is set as the “graphic” property on the label.

This approach works perfectly well but with JavaFX there is a more elegant way. You can put the image definition into a CSS file, making it easy for you and / or others to replace it (the marketing department has decided to change the corporate identity once again).

The same result as above can be achieved this way:

1
2
3
4
5
6
7
8
import javafx.scene.control.Label;
 
public class CSSLabel extends Label {
 
    public CSSLabel() {
        getStyleClass().add("folder-icon");
    }
}

Now you obviously need a CSS file as well:

1
2
3
.folder-icon {
    -fx-graphic: url("image.gif");
}

And in your application you need to add the stylesheet to your scene graph. Here we are adding it to the scene.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
public class MyApplication extends Application {
 
    public void start(Stage primaryStage) throws
                                     Exception {
        CSSLabel label = new CSSLabel();
        label.setText("Folder");
        label.setAlignment(Pos.CENTER);
         
        Scene scene = new Scene(label);
        scene.getStylesheets().add(MyApplication.class.
              getResource("test.css").toExternalForm());
         
        primaryStage.setScene(scene);
         
        primaryStage.setTitle("Image Example");
        primaryStage.setWidth(250);
        primaryStage.setHeight(100);
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

With this approach you have a clean separation of your controls and their apperance and you allow for easy customization as well.

Reference: JavaFX Tip 12: Define Icons in CSS from our JCG partner Dirk Lemmermann at the Pixel Perfect blog.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button