0

How to set an icon for javafx application?

When I add icon to my project.it occurs an error.

I am developing an application in JavaFX and I am trying to change the icon of my application, according to the code below:

My code:

public class DEMO extends Application {



@Override
public void start(Stage stage) throws Exception {
    //Parent root = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
    Parent root = (Parent) fxmlLoader.load();
    MainScreenController mainScreenController = (MainScreenController) fxmlLoader.getController();
    mainScreenController.setMainScreenController(mainScreenController);
 
    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.setTitle("demo");
    stage.getIcons().add(new Image("/icon.png"));
    stage.setOnCloseRequest(e -> {
        Platform.exit();
        System.exit(0);
    });
    stage.show();
}




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

}

}

When I run it comes an error.

Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at 
  java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at 
  javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at  java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
 Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at 
 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
 Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:617)
at remotecontrolpc.RemoteControlPC.start(RemoteControlPC.java:37)
at 
  javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at 
 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
  Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
... 11 more
  Exception running application remotecontrolpc.RemoteControlPC

  Process finished with exit code 1 

Why this error comes ?

How can I solve this error?

Pradeep
  • 19
  • 1
  • 6
  • see answer here, https://stackoverflow.com/a/10122335/3625077 basically you need load image as a file: or the getResourceAsStream on your class – b3tuning Jul 30 '20 at 06:11
  • I tried this `stage.getIcons().add(new Image("file:icon.png")); stage.getIcons().add(new Image(DEMO.class.getResourceAsStream("icon.png")));` But , the same error. – Pradeep Jul 30 '20 at 06:47
  • 1
    Does this answer your question? [How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?](https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other) – kleopatra Jul 30 '20 at 07:49
  • Another issue is that one should not use the getResourceAsStream method to load icons because that way the @2x mechanism does not work. Use getResource().toExternalForm() instead. For details see: https://dlsc.com/2017/08/29/javafx-tip-27-hires-retina-icons/ – mipa Jul 30 '20 at 12:29

0 Answers0