JavaFX Application Icon
Asked Answered
S

17

210

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

Selfdetermination answered 12/4, 2012 at 10:43 Comment(0)
B
297

Assuming your stage is "stage" and the file is on the filesystem:

stage.getIcons().add(new Image("file:icon.png"));

As per the comment below, if it's wrapped in a containing jar you'll need to use the following approach instead:

stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("icon.png")));
Bogtrotter answered 12/4, 2012 at 11:8 Comment(10)
Additionally, if the icon is to be installed as an application shortcut and you are using WebStart as the deployment technology then you might want to set the appropriate icon/shortcut/desktop settings in your jnlp file: docs.oracle.com/javase/7/docs/technotes/guides/javaws/…Agency
You can add multiple images of different sizes and JavaFX will pick the one that fits best. Because you have different sizes in task bar and different in title bar.Natalienatalina
I think this does only work when the file is in the file system, for the more common situation where the icon is wrapped inside the jar file and in the classpath, this was not working for me. One of the other solutions using stage.getIcons().add( new Image( <yourclassname>.class.getResourceAsStream( "icon.png" ))); works.Gaffney
Please not, that at least on Ubuntu (Unity) the icon is only used for the window decoration, but not for the launcher. To have the icon visible (instead of the ugly question mark), you'd need to specify appropriate .dektop file. See my description hereStrow
Didn't find it that intuitive but make sure that you have the image URL prefixed with file, e.g, if you have the logo in the res folder under your project root: stage.getIcons().add(new Image("file:"+String.valueOf(Paths.get(System.getProperty("user.dir"),"res","logo.png"))));Auricular
this changes only icon on title bar ie top left side on window. how to change icon of application when it is on desktop as shortcut? and on taskbar?Hyacinthie
I m trying to add multiple icons . However only the first one appears ! Any idea ? ThanksRachmaninoff
@sushmithashenoy The multiple icons are meant to be the same icons of different resolution, and JFX will pick the best one. It doesn't support showing multiple icons simultaneously.Bogtrotter
I have a similar question to @TejpalBh. I've searched this whole page for any mention of "Mac" and found none. Is this supposed to change the icon that shows up in the MacOS Dock/task switcher? I have tested it, and apparently it does not. Should a separate question be made for that?Origami
Answering my own question: Apparently this is the way to change the MacOS dock icon for java apps: https://mcmap.net/q/128856/-how-do-you-change-the-dock-icon-of-a-java-program I tested it and it works for meOrigami
C
86

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.

Cryoscopy answered 10/10, 2012 at 21:10 Comment(2)
'<yourclassname>.class' could be replace by 'getClass()'Homogenous
If you want to put your icon in the resources directory you'll need to change <yourclassname>.class.getResourceAsStream( "icon.png" )) to <yourclassname>.class.getClassLoader().getResourceAsStream("icon.png))Baobaobab
K
86

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!!

Knickerbockers answered 4/3, 2013 at 16:30 Comment(4)
Does this support svg?Pluck
@qed, I don't think SVG is supported, but I'm not 100% sure. You can test it.Knickerbockers
I just tried using a .svg file for the JavaFX application icon. It did not work. No error was provided, but JavaFX used its default logo instead of my custom one.Linzer
@Pluck SVG is not supported.Pieeyed
A
12

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")));
Autochthon answered 25/10, 2016 at 12:26 Comment(0)
Y
11

you can add it in fxml. Stage level

<icons>
    <Image url="@../../../my_icon.png"/>
</icons>
York answered 5/4, 2017 at 8:30 Comment(0)
R
3
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

Regimentals answered 25/5, 2016 at 11:18 Comment(0)
N
2

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).

Nyx answered 22/3, 2017 at 13:51 Comment(1)
It isn't good practice to place resources in your java packages. It is recommended to place them in your resources folder.Provinciality
C
2
stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));

images folder need to be in Resource folder.

Cooker answered 28/11, 2017 at 12:19 Comment(1)
This answer is completely unnecessary since another answer already explained the same.Wheels
I
1
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

Insphere answered 30/9, 2016 at 15:33 Comment(0)
E
1

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

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

Eads answered 3/12, 2018 at 11:21 Comment(0)
B
0
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.

Barbellate answered 17/7, 2017 at 13:24 Comment(1)
This answer is completely unnecessary since another answer already explained the same.Wheels
U
0

I used this in my application

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

Here window is the stage.

Undermost answered 11/12, 2017 at 4:31 Comment(0)
S
0

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.

Strother answered 4/3, 2019 at 14:11 Comment(0)
M
0

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")));
    }
}
Makhachkala answered 27/8, 2019 at 20:3 Comment(0)
F
-1

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

Fourth answered 28/11, 2018 at 19:11 Comment(0)
D
-2

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).

Dude answered 16/10, 2012 at 9:25 Comment(1)
isn't that exactly what has been posted above?Bottali
D
-2

I tried this and it works:

stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));
Diecious answered 17/8, 2016 at 20:30 Comment(1)
actually, .. no: dots are not supported in resource lookup, please read the api docCheyennecheyne

© 2022 - 2024 — McMap. All rights reserved.