JavaFX Application Icon Setup

JavaFX Application Icon Tutorial

All programs require a nice-looking icon or logo on the taskbar and the window to make it look professional. JavaFX provides easy ways to set up an application icon using any image of your choice, whether it be JPG or PNG or something else. In this tutorial, let’s see how to set up an icon for the application

Set JavaFX Window/Stage icon from an image file

The usual method of attaching an icon to the JavaFX application is that, you simply include the icon image inside the JAR file as a resource and then use it. Let’s see how you can do that.

Step 1: Add the icon to the resource directory

First, add the image into the resource directory. You can use any supported image formats, like PNG, JPEG etc.

JavaFX Add Image Resource

Step 2: Attach the icon to the application window

Now, let’s attach the image from the resource directory to the window, aka stage. The following code snippets show how to do that in an example program.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFXIconExample extends Application {

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new StackPane(), 320, 320);    

    //Attach the icon to the stage/window
    stage.getIcons().add(new Image(JavaFXIconExample.class.getResourceAsStream("/logo.png")));

    stage.setTitle("Sample program!");
    stage.setScene(scene);
    stage.show();
  }

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

So in the above example, we are attaching the icon to the primary stage (also known as root stage). If you want to add logo to the other stages you create, you can repeat the same process again to add icons to them as well, with just one line of code.

stage.getIcons().add(new Image(JavaFXIconExample.class.getResourceAsStream("/logo.png")));

JavaFX Application With Custom Icon

Set icon from an image URL

Now, let’s say you want to add the image dynamically from a URL instead of adding from an image file, that is also easily possible. This can be done with just one line of code, as shown below.

stage.getIcons().add(new Image("https://genuinecoder.com/wp-content/uploads/2022/06/genuine_coder-3.png"));

In this example, I have used genuine coder logo to dynamically load the application icon. The advantage of using the logo from a URL is that, you can change the logo in the server without touching your program once it is rolled out to your customers.

Conclusion

JavaFX provides a one-line option to attach images/logo to the windows. The logo image can be loaded from an image file or from a URL.

Muhammed Afsal Villan
Muhammed Afsal Villan is an experienced full-stack developer, specialized in desktop and mobile application development. He also regularly publishes quality tutorials on his YouTube channel named 'Genuine Coder'. He likes to contribute to open-source projects and is always enthusiastic about new technologies.

8 COMMENTS