Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Icon on Stage in JavaFX

I wanted to know how should I set icons on javaFX stage. I have found this method, but it did not work properly.

  stage.getIcons().add(new Image(iconImagePath));

stage is an instance of javafx.stage.Stage, and I have imported javafx.scene.image.Image. This is the exception which we receive:

Invalid URL: Invalid URL or resource not found

Also, there is nothing wrong with the iconImagePath, its value is "G:/test.jpg" and there is a jpg file in the G drive named test. In addition, when we use ImageIO to read the same URL we can do it easily.

like image 439
Siavash Avatar asked Nov 20 '13 11:11

Siavash


People also ask

How do you display a stage in JavaFX?

Showing a Stage The difference between the JavaFX Stage methods show() and showAndWait() is, that show() makes the Stage visible and the exits the show() method immediately, whereas the showAndWait() shows the Stage object and then blocks (stays inside the showAndWait() method) until the Stage is closed.

What is a stage object in JavaFX?

The JavaFX Stage class is the top level JavaFX container. The primary Stage is constructed by the platform. Additional Stage objects may be constructed by the application. Stage objects must be constructed and modified on the JavaFX Application Thread.


2 Answers

If you're using eclipse make sure you add the folder that has the image to the build path. this way you can refer to the image with its name with no problems.enter image description here

like image 105
Mahmoud Ibrahim Avatar answered Sep 18 '22 11:09

Mahmoud Ibrahim


use

stage.getIcons().add(new Image(("file:logo.png")));

and put the image logo.png in root of your project ( at same directory where src )

like image 33
Maher Abuthraa Avatar answered Sep 17 '22 11:09

Maher Abuthraa