Originally from: http://stackoverflow.com/questions/10275841/ how-to-change-the-icon-on-the-title-bar-of-a-stage-in-java-fx-2-0-of-my-applicat/15206407#15206407
Import Javafx.application.application;import Javafx.scene.scene;import Javafx.scene.layout.stackpane;import Javafx.scene.image.image;import Javafx.stage.stage;public class Stackoverflowicon extends application{ @ Override public void Start (stage stage) { Stackpane root = new Stackpane (); Scene scene = new Scene (root, +); Set icon stage.geticons (). Add (New Image ("/path/to/stackoverflow.jpg")); Set title stage.settitle ("wow!! StackOverflow Icon "); Stage.setscene (scene); Stage.show (); } /** * @param args the command line arguments * /public static void main (string[] args) { launch (args);} }
Run:
JavaFX 8 Upgrade
The above code still works and there is no need to modify it. java 1.8 (1.8.0_31) environment for testing, testing. The path supports either local or remote.
stage.getIcons().add(new Image("/path/to/javaicon.png"));
Or
stage.getIcons().add(new Image("https://example.com/javaicon.png"));
I hope it helps you.
--------------------------------------------------------------------------------------------------------------- ------------------------------
You can also load images from within the classpath like this:
new Image(XYZ.class.getResourceAsStream("/xyz.png"))
XYZ is the class name (or the class name of the class where you are loading the picture) Xyz.png is your picture name. Put it in your classpath path or inside the jar package.
If your icon and class are placed in the same package, you can omit/
JavaFx changes the icon of the title bar of the stage