Try the following, this worked for me:
Image image = new Image(XYZ.class.getClassLoader().getResource("my.png").toExternalForm());
stage.getIcons().add(image);
If you're using Linux, you may be interested in this question.
Here is a MWE:
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.getIcons().add(new Image(Main.class.getClassLoader().getResource("sample/icon.png").toExternalForm()));
stage.setScene(new Scene(new Group(), 300, 275));
stage.show();
}
public static void main(String[] args) {launch(args);}
}
This assumes that there is an image icon.png
in directory sample
(which is the same directory as the Main
class). As you can see, the icon is both in the task bar and in the title bar of the application:
