0

I wrote this code:

public void start(Stage stage) throws Exception {
    Group root = new Group();
    Scene scene = new Scene(root);

    Image icon = new Image("file:/home/ernestosmr/Documents/java_projects/helloFX/src/java.png", 64,64,true,true);
    stage.getIcons().add(icon);
    stage.setTitle("Stage demo program wooot");

    stage.setScene(scene);
    stage.show();
}

I have my java.png on my src folder but still is not showing on my stage when I run this code. I try different things but it's not working. I'm using IntelliJ IDEA.

I want the icon appears on my stage when I run the code.

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Hi . Please add stacktrace – Giovanni Contreras Apr 19 '23 at 01:54
  • 1
    In what way is it not working? Do you get an error? Does the icon simply not show? – Slaw Apr 19 '23 at 02:19
  • Just to start with, keep your java.png in the same folder as your class and initialize image as below and check once. `Image icon = new Image(getClass().getResource("java.png").toExternalForm());` – Sai Dandem Apr 19 '23 at 04:27
  • I added some thoughts on icons in an update to this [old question on setting icons](https://stackoverflow.com/a/16784326/1155209). – jewelsea Apr 19 '23 at 08:17

1 Answers1

1

It would be better to provide whole source code and error message for better understanding.

  1. In your given code, which is not full! main body is missing

  2. Assume that you have full source code, and you are only showing the main parts. In this case double check the java.png location in your given path: /home/ernestosmr/Documents/java_projects/helloFX/src/

  3. But anyway I'm giving you sample to show the icon image in the stage. (java.png is located in the following path: C:/Users/Downloads/java.png)

    public class Test extends Application {
    
     public void start(Stage stage) throws Exception {
         Group root = new Group();
         Scene scene = new Scene(root);
    
         Image icon = new Image("file:C:/Users/Downloads/java.png");
         stage.getIcons().add(icon);
         stage.setTitle("Stage demo program wooot");
    
         stage.setScene(scene);
         stage.show();
     }
    
     public static void main(String[] args) {launch(args);}
    }
    

Output is as following:

And java.png image is as following: enter image description here

enter image description here

  • I haven't got a Windows box to test it at the moment, but, on Windows, does the custom icon also show in the Taskbar? (on OS X the custom icon does not show in the dock). On Windows does the icon only show when the title text is also set? (last time I tested on OS X, that was the case). Please add the JavaFX version and OS type and version you tested this on, as the behavior may change between versions. Add the actual icon you used to the example so it is complete. Thanks. – jewelsea Apr 19 '23 at 08:28
  • Hi there, sorry what do you mean by 'add the actual icon you used to the example?'. I didn't ask the question. On Windows whether there is a title or not, you can set the icon. – Farkhod Abdukodirov Apr 19 '23 at 23:46
  • You used the image `java.png` to create your answer, it looks like some kind of Java cup icon. It would have been necessary to produce the screenshot you added. [Provide the image in your answer](https://meta.stackexchange.com/questions/75491/how-to-upload-an-image-to-a-post) (the same way you provided the output screenshot) so that somebody can replicate the answer easily and exactly. – jewelsea Apr 20 '23 at 00:53
  • 1
    now I see, sure I'll add the image as well. Yeah you are right it is the java cup icon. I made it java.png cos owner of the question could understand easily, because he also used the java.png in his question. – Farkhod Abdukodirov Apr 20 '23 at 00:56