14

I have tried the

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

But I don't know what is going wrong ..

Please help. Thanks in advance.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
Bipin Bhandari
  • 652
  • 4
  • 9
  • 23

6 Answers6

53

Full program for starters :) This program set Stack Overflow Icon.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class StackoverflowIcon extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        // set icon
        stage.getIcons().add(new Image("/path/to/stackoverflow.jpg"));
        stage.setTitle("Wow!! Stackoverflow Icon");
        stage.setScene(new Scene(root, 300, 250));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Output Screnshot

JavaFX Screenshot

Updated for JavaFX 8

No need to change the code. It still works fine. Tested and verified in Java 1.8(1.8.0_45). Path can be set to local or remote both are supported.

stage.getIcons().add(new Image("/path/to/javaicon.png"));

OR

stage.getIcons().add(new Image("https://example.com/javaicon.png"));

enter image description here

Hope it helps. Thanks!!

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
5

You can load the image from the classpath like this:

new Image(XYZ.class.getResourceAsStream("/xyz.png"))

where XYZ is some classname (maybe the one you're loading the image from) and xyz.png is the name of your image file, put in a directory (or JAR file) included in your classpath.

If you like to put the image next to the source file, you have to omit the / character. Your IDE needs to be configured to copy ressources (like *.png) from src to bin directory, then. But this is supposed to be the standard behaviour.

FRB
  • 123
  • 2
  • 8
3

For those who had a problem with:

Invalid URL: Invalid URL or resource not found

The best solution is to make new package i.e image.icons and move there your .png image. Then you just need to write:

Image image = new Image("/image/icons/list.png");
primaryStage.getIcons().add(image);

I hope this helps somebody!

Dawid
  • 89
  • 5
1

Does your image have correct size? Javadoc states:

public final ObservableList getIcons()

Gets the icon images to be used in the window decorations and when minimized. The images should be different sizes of the same image and the best size will be chosen, eg. 16x16, 32,32.

Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • Thanks Sergey but image size doesn't affect that i think so. but yeah i finally got the solution by setting the properties of standalone working directory to package where my main and image is placed. – Bipin Bhandari Apr 26 '12 at 10:17
  • oh, image finding was a problem, great you've find out it. There were a bunch of similar issues, e.g.: http://stackoverflow.com/questions/10062270/how-to-target-a-file-a-path-to-it-in-java-javafx – Sergey Grinev Apr 26 '12 at 11:32
1

The solution i found by setting the properties of standalone working directory to package where my main and image is placed.

Bipin Bhandari
  • 652
  • 4
  • 9
  • 23
1

Dont forget to do the import

import javafx.scene.image.Image;

Image icon = new Image(getClass().getResourceAsStream("myicon.png"));
stage.getIcons().add(icon);

Replace "myicon.png" with your icon. In this case it's in the same folder as your java class.

AlexVogel
  • 10,601
  • 10
  • 61
  • 71
daviek19
  • 21
  • 1
  • 5