How to Change the icon on the title bar of a stage in java fx 2.0 of my application [duplicate]
Asked Answered
P

6

16

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.

Plan answered 23/4, 2012 at 6:32 Comment(4)
Append the error message to your question if any.Picket
I'm guessing attuncore.jpg was not found.Fording
The problem is that i am not getting any error.Plan
There is a similar question in SO.Check out the below Link JavaFX Application IconDexamethasone
L
55

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!!

Loriloria answered 4/3, 2013 at 16:36 Comment(3)
I wonder how come no body up voted you, thanks for the answerCodfish
Does the size matter? c:Whilom
Perfect answer, and size does not appear to matter.Macaque
L
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.

Lithograph answered 10/10, 2012 at 20:22 Comment(0)
I
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!

Ianthe answered 22/3, 2017 at 13:38 Comment(1)
"file:image/icons/list.png" is another solution.Philately
R
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.

Rationalism answered 26/4, 2012 at 8:47 Comment(2)
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.Plan
oh, image finding was a problem, great you've find out it. There were a bunch of similar issues, e.g.: #10062770Rationalism
P
1

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

Plan answered 7/5, 2012 at 9:5 Comment(0)
S
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.

Sphinx answered 2/9, 2014 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.