Is it possible to change the application icon using JavaFX, or does it have to be done using Swing?
stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));
images folder need to be in Resource folder.
Full program for starters :) This program sets icon for StackOverflowIcon.
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
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"));
Hope it helps. Thanks!!
I used this in my application
Image icon = new Image(getClass().getResourceAsStream("icon.png"));
window.getIcons().add(icon);
Here window is the stage.
stage.getIcons().add(new Image("/images/logo_only.png"));
It is good habit to make images folder in your src folder and get images from it.
If you got Invalid URL or resource not found
put your icon.png in the "bin" folder in your workspace.
you can add it in fxml. Stage level
<icons>
<Image url="@../../../my_icon.png"/>
</icons>