JavaFX
application icon
UI development
Java programming
desktop applications

How to set a JavaFX application icon

Codemia Knowledge Hub ·

Load the image with javafx.scene.image.Image and add it to the stage's icon list: stage.getIcons().add(icon). For a packaged JAR you can also define the icon in a deployment descriptor file.

Setting the icon on the Stage

The Stage is the primary window of a JavaFX application, and its icons are set in code:

java
1import javafx.application.Application;
2import javafx.scene.Scene;
3import javafx.scene.image.Image;
4import javafx.scene.layout.StackPane;
5import javafx.stage.Stage;
6
7public class IconExample extends Application {
8
9    @Override
10    public void start(Stage stage) {
11        // Create a layout
12        StackPane root = new StackPane();
13        Scene scene = new Scene(root, 400, 300);
14
15        // Set the application icon
16        Image icon = new Image(getClass().getResourceAsStream("/path/to/icon.png"));
17        stage.getIcons().add(icon);
18
19        // Set other stage properties
20        stage.setTitle("JavaFX Icon Example");
21        stage.setScene(scene);
22        stage.show();
23    }
24
25    public static void main(String[] args) {
26        launch(args);
27    }
28}
  • Image comes from the javafx.scene.image package.
  • getClass().getResourceAsStream("/path/to/icon.png") loads the file from your resources directory.
  • stage.getIcons().add(icon) attaches it to the window.

Setting the icon in a deployment descriptor

For JavaFX applications packaged as JAR files, you can define the icon in the deployment descriptor (for example app.xml), so it is distributed with the application.

Icon size and format

  • Sizes: provide several, such as 16x16, 32x32 and 48x48 pixels, plus larger images for high DPI displays.
  • Format: PNG is preferred for its lossless compression and transparency support.
  • Design: keep the icon consistent with your brand (colors, logo), make it stand out in the taskbar, and make sure it is recognizable across cultures if you distribute internationally.

Icon not displaying

  • Check the path: confirm the path is correct and the file is included in the project's resources.
  • Check the format: confirm the file is a supported format (typically PNG) and not corrupted.
  • Check the stage: make sure the Stage is visible and not hidden behind another window.
AspectDetails
Icon Setting Methods1. Programmatically with Stage object 2. Using Deployment Descriptor (e.g., app.xml)
Recommended Icon SizesInclude 16x16, 32x32, 48x48, and larger for high DPI screens
Supported FormatsPreferably PNG due to transparency support
Localization ConsiderationsIcons should be easily recognizable across different cultures
TroubleshootingVerify image path, format, and stage visibility

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms