JavaFX Application Icon
Solution 1
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")));
Solution 2
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.
Solution 3
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
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"));
Hope it helps. Thanks!!
Solution 4
you can add it in fxml. Stage level
<icons>
<Image url="@../../../my_icon.png"/>
</icons>
Solution 5
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")));
Related videos on Youtube
Sebb77
Updated on December 13, 2020Comments
-
Sebb77 over 3 years
Is it possible to change the application icon using JavaFX, or does it have to be done using Swing?
-
jewelsea about 12 yearsAdditionally, 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/…
-
XXL over 11 yearsisn't that exactly what has been posted above?
-
drzymala over 11 yearsYou 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.
-
qed over 9 yearsDoes this support svg?
-
Madan Sapkota almost 9 years@qed, I don't think SVG is supported, but I'm not 100% sure. You can test it.
-
k_o_ about 8 yearsI 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. -
dzim about 8 yearsPlease 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 here -
L. Guthardt over 6 yearsThis answer is completely unnecessary since another answer already explained the same.
-
L. Guthardt over 6 yearsThis answer is completely unnecessary since another answer already explained the same.
-
Ko Ga almost 6 yearsDidn'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 theres
folder under your project root:stage.getIcons().add(new Image("file:"+String.valueOf(Paths.get(System.getProperty("user.dir"),"res","logo.png"))));
-
Jim Fred over 5 years'<yourclassname>.class' could be replace by 'getClass()'
-
TejpalBh over 5 yearsthis 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?
-
Thibstars over 5 yearsIt isn't good practice to place resources in your java packages. It is recommended to place them in your
resources
folder. -
sushmitha shenoy almost 5 yearsI m trying to add multiple icons . However only the first one appears ! Any idea ? Thanks
-
Michael Berry almost 5 years@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.
-
Simon Diemert almost 4 yearsI 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.
-
kleopatra almost 4 yearsactually, .. no: dots are not supported in resource lookup, please read the api doc
-
Matt Groth about 3 yearsI 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?
-
Matt Groth about 3 yearsAnswering my own question: Apparently this is the way to change the MacOS dock icon for java apps: stackoverflow.com/a/6006190/6596010 I tested it and it works for me
-
Tom Rutchik almost 3 yearsIf 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))