Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting stage icon

Tags:

java

javafx

My first intention was to set TextInputDialog icon. But I started from setting stage icon. I saw couple SO questions with marvelous answers containing usually 2 lines of code.

First I tried to put this icon to /resources/icons but exception "Invalid URL or resource not found" appeared. To be sure I don't make any mistake writing file path I moved this icon to /source/sample directory. I use code (I will post whole code):

public void start(Stage stage) throws Exception {

    FXMLLoader loaderModyfikacjaKonfiguracji = new FXMLLoader(getClass().getResource("FXMLModyfikacjaKonfiguracji.fxml"));
    Parent root = loaderModyfikacjaKonfiguracji.load();
    stage.setTitle("Modyfikacja konfiguracji");
    Image image = new Image("file:icon.png");
    //stage.getIcons().removeAll();
    stage.getIcons().add(image);

    ControllerModyfikacjaKonfiguracji controllerModyfikacjaKonfiguracji = loaderModyfikacjaKonfiguracji.getController();

    stage.setScene(new Scene(root, 510, 700));
    stage.show();
}

Everywhere it looks so simple to set icon. I also tried .jpg. not using file: throws exception, using file: compiles but I see no effect of changed icon. What am I doing wrong or where is the problem?

like image 525
Urle Avatar asked Dec 18 '25 06:12

Urle


1 Answers

I've successfully used this to set an icon before

primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("AppIcon.png")));

In my case, the application fxml file and AppIcon.png are in the same directory.

If you dont want to go that route, i would suggest trying

 Image image = new Image("file:./icon.png");

But that's a guess.

like image 151
andrew-g-za Avatar answered Dec 19 '25 19:12

andrew-g-za