Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 969
Bipin Bhandari Avatar asked Apr 23 '12 06:04

Bipin Bhandari


People also ask

How do you add a button to a scene in Javafx?

You can create a Button by instantiating the javafx. scene. control. Button class of this package and, you can set text to the button using the setText() method.


2 Answers

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

like image 181
Madan Sapkota Avatar answered Sep 24 '22 17:09

Madan Sapkota


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.

like image 32
Frank Bruch Avatar answered Sep 20 '22 17:09

Frank Bruch