JavaFX Application Icon

172,305

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

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

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")));
Share:
172,305

Related videos on Youtube

Sebb77
Author by

Sebb77

Updated on December 13, 2020

Comments

  • Sebb77
    Sebb77 over 3 years

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

  • jewelsea
    jewelsea about 12 years
    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/…
  • XXL
    XXL over 11 years
    isn't that exactly what has been posted above?
  • drzymala
    drzymala over 11 years
    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.
  • qed
    qed over 9 years
    Does this support svg?
  • Madan Sapkota
    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_
    k_o_ about 8 years
    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.
  • dzim
    dzim about 8 years
    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 here
  • L. Guthardt
    L. Guthardt over 6 years
    This answer is completely unnecessary since another answer already explained the same.
  • L. Guthardt
    L. Guthardt over 6 years
    This answer is completely unnecessary since another answer already explained the same.
  • Ko Ga
    Ko Ga almost 6 years
    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("u‌​ser.dir"),"res","log‌​o.png"))));
  • Jim Fred
    Jim Fred over 5 years
    '<yourclassname>.class' could be replace by 'getClass()'
  • TejpalBh
    TejpalBh over 5 years
    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?
  • Thibstars
    Thibstars over 5 years
    It isn't good practice to place resources in your java packages. It is recommended to place them in your resources folder.
  • sushmitha shenoy
    sushmitha shenoy almost 5 years
    I m trying to add multiple icons . However only the first one appears ! Any idea ? Thanks
  • Michael Berry
    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
    Simon Diemert almost 4 years
    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.
  • kleopatra
    kleopatra almost 4 years
    actually, .. no: dots are not supported in resource lookup, please read the api doc
  • Matt Groth
    Matt Groth about 3 years
    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?
  • Matt Groth
    Matt Groth about 3 years
    Answering 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
    Tom Rutchik almost 3 years
    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))