1

I have tried the

new Image(XYZ.class.getResourceAsStream("my.png"))

and

stage.getIcons().add(new Image("my.jpg")); 

and stage.getIcons().add(new Image("/path/to/my.jpg"));

i can see icon at task bar but i can not see at app title bar my javafx jar version is 1.8.45 thank you

Gaali Prabhakar
  • 583
  • 6
  • 23

1 Answers1

3

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:

Screenshot

Community
  • 1
  • 1
Aerus
  • 4,332
  • 5
  • 43
  • 62