[java] JavaFX Application Icon

Is it possible to change the application icon using JavaFX, or does it have to be done using Swing?

This question is related to java javafx javafx-2 javafx-8 stage

The answer is


If you have have a images folder and the icon is saved in that use this

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/images/comparison.png")));

and if you are directly using it from your package which is not a good practice use this

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("comparison.png")));

and if you have a folder structure and you have your icon inside that use

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("../images/comparison.png")));

stage.getIcons().add(new Image("/images/logo_only.png"));

It is good habit to make images folder in your src folder and get images from it.


I tried this and it totally works. The code is:

stage.getIcons().add(
   new Image(
      <yourclassname>.class.getResourceAsStream( "icon.png" ))); 

icon.png is under the same folder as the source files.


stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png" )));

You can add more than one icon with different sizes using this method.The images should be different sizes of the same image and the best size will be chosen. eg. 16x16, 32,32


I used this in my application

Image icon = new Image(getClass().getResourceAsStream("icon.png"));
window.getIcons().add(icon);

Here window is the stage.


You can easily put icon to your application using this code line

stage.getIcons().add(new Image("image path") );


Toggle icons in runtime:

In addition to the responses here, I found that once you have assigned an Icon to your application by the first time you cannot toggle it by just adding a new icon to your stage (this would be helpful if you need to toggle the icon of your app from on/off enabled/disabled).

To set a new icon during run time use the getIcons().remove(0) before trying to add a new icon, where 0 is the index of the icon you want to override like is shown here:

//Setting icon by first time (You can do this on your start method).
stage.getIcons().add(new Image(getClass().getResourceAsStream("enabled.png")));

//Overriding app icon with a new status (This can be in another method)
stage.getIcons().remove(0);
stage.getIcons().add(new Image(getClass().getResourceAsStream("disabled.png")));

To access the stage from other methods or classes you can create a new static field for stage in you main class so can access it from out of the start() method by encapsulating in on a static method that you can access from anywhere in your app.

public class MainApp extends Application {
    private static Stage stage;
    public static Stage getStage() { return stage; }

    @Override public void start(Stage primaryStage) {
        stage = primaryStage
        stage.getIcons().add(new Image(getClass().getResourceAsStream("enabled.png")));
    }
}

public class AnotherClass {
    public void setStageTitle(String newTitle) {
        MainApp.getStage().setTitle(newTitle);
        MainApp.getStage().getIcons().remove(0);
        MainApp.getStage().getIcons().add(new Image(getClass().getResourceAsStream("disabled.png")));
    }
}

Another easy way to insert your own icon on the title bar in JavaFX is to add the image to your primary stage using the following method:

Image ico = new Image("resources/images/iconLogo.png");
stage.getIcons().add(ico);

Make sure your import javafx.scene.image.Image (if using an ide like netbeans this should be automatically done for you).


If you run the jar file, the code specified by Michael Berry will change the icon in the title bar and in the taskbar. Shortcut icon cannot be changed.

If you run a native program compiled with com.zenjava, You must add a link to the program icon:

<plugin>
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.8.3</version>
    <configuration>
    ...
        <bundleArguments>
            <icon>${project.basedir}/src/main/resources/images/filename.ico</icon>
        </bundleArguments>
    </configuration>
</plugin>

This will add an icon to the shortcut and taskbar.


I tried this and it works:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png")));

If your icon.png is in resources dir and remember to put a '/' before otherwise it will not work


you can add it in fxml. Stage level

<icons>
    <Image url="@../../../my_icon.png"/>
</icons>

If you got Invalid URL or resource not found put your icon.png in the "bin" folder in your workspace.


stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));

images folder need to be in Resource folder.


What do you think about creating new package i.e image.icons in your src directory and moving there you .png images? Than you just need to write:

Image image = new Image("/image/icons/nameOfImage.png");
primaryStage.getIcons().add(image);

This solution works for me perfectly, but still I'm not sure if it's correct (beginner here).


Full program for starters :) This program sets icon for StackOverflowIcon.

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

public class StackoverflowIcon extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        // set icon
        stage.getIcons().add(new Image("/path/to/stackoverflow.jpg"));
        stage.setTitle("Wow!! Stackoverflow Icon");
        stage.setScene(new Scene(root, 300, 250));
        stage.show();
    }

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

Output Screnshot

JavaFX Screenshot

Updated for JavaFX 8

No need to change the code. It still works fine. Tested and verified in Java 1.8(1.8.0_45). Path can be set to local or remote both are supported.

stage.getIcons().add(new Image("/path/to/javaicon.png"));

OR

stage.getIcons().add(new Image("https://example.com/javaicon.png"));

enter image description here

Hope it helps. Thanks!!


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to javafx

IntelliJ can't recognize JavaFX 11 with OpenJDK 11 Error: JavaFX runtime components are missing, and are required to run this application with JDK 11 JavaFX FXML controller - constructor vs initialize method Why is JavaFX is not included in OpenJDK 8 on Ubuntu Wily (15.10)? cannot resolve symbol javafx.application in IntelliJ Idea IDE javac: invalid target release: 1.8 How to change the color of text in javafx TextField? Issue with background color in JavaFX 8 What's the location of the JavaFX runtime JAR file, jfxrt.jar, on Linux? How to create a popup windows in javafx

Examples related to javafx-2

How to create a popup windows in javafx Is it possible to run JavaFX applications on iOS, Android or Windows Phone 8? JavaFX "Location is required." even though it is in the same package JavaFX and OpenJDK JavaFX Panel inside Panel auto resizing JavaFX open new window JavaFX: How to get stage from controller during initialization? How to close a JavaFX application on window close? JavaFX Application Icon How to attach source or JavaDoc in eclipse for any jar file e.g. JavaFX?

Examples related to javafx-8

Issue with background color in JavaFX 8 How to create a popup windows in javafx JavaFX: How to get stage from controller during initialization? How to close a JavaFX application on window close? JavaFX 2.1 TableView refresh items JavaFX Application Icon

Examples related to stage

JavaFX Location is not set error message JavaFX: How to get stage from controller during initialization? JavaFX Application Icon