Error trying to change Javafx application icon

Asked

Viewed 81 times

0

Hello. I am developing an application in Javafx and I am trying to change the icon of my application according to the code below:

public class Login extends Application 

{

Image applicationIcon = new Image("/src/Icons/message.png");

@Override
public void start(Stage primaryStage) 

{

try 

    {
        Parent root = FXMLLoader.load(getClass().getResource("../FXML/Login.fxml"));
        Scene scene = new Scene(root);
        primaryStage.setTitle("Login");

        primaryStage.getIcons().add(new Image(Login.class.getResourceAsStream("src/Icons/message.png")));           
        primaryStage.getIcons().add(applicationIcon);
        primaryStage.setScene(scene);
        primaryStage.show();


    } 

catch (IOException e) 

    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

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

}

However, when running the script to change the application icon, the following error occurs:

Exception in Application constructor java.lang.reflect.Invocationtargetexception at sun.reflect..Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect..Nativemethodaccessorimpl.invoke(Unknown Source) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.sun.javafx.application.Launcherimpl.launchApplicationWithArgs(Launcherimpl.java:389) at com.sun.javafx.application.Launcherimpl.launchApplication(Launcherimpl.java:328) at sun.reflect..Nativemethodaccessorimpl.invoke0(Native Method) at sun.reflect..Nativemethodaccessorimpl.invoke(Unknown Source) at sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.launcher.Launcherhelper$Fxhelper.main(Unknown Source) Caused by: java.lang.Runtimeexception: Unable to Construct Application instance: class application. Login at com.sun.javafx.application.Launcherimpl.launchApplication1(Launcherimpl.java:907) at com.sun.javafx.application.Launcherimpl.lambda$launchApplication$159(Launcherimpl.java:182) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.reflect.Invocationtargetexception at sun.reflect.Nativeconstructoraccessorimpl.newInstance0(Native Method) at sun.reflect.Nativeconstructoraccessorimpl.newInstance(Unknown Source) at sun.reflect.Delegatingconstructoraccessorimpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.sun.javafx.application.Launcherimpl.lambda$launchApplication1$165(Launcherimpl.java:819) at com.sun.javafx.application.Platformimpl.lambda$runAndWait$179(Platformimpl.java:326) at com.sun.javafx.application.Platformimpl.lambda$null$177(Platformimpl.java:295) at java.security.Accesscontroller.doPrivileged(Native Method) at com.sun.javafx.application.Platformimpl.lambda$runLater$178(Platformimpl.java:294) at com.sun.Glass.ui.Invokelaterdispatcher$Future.run(Invokelaterdispatcher.java:95) at com.sun.Glass.ui.win.WinApplication. _runLoop(Native Method) at com.sun.Glass.ui.win.WinApplication.lambda$null$152(Winapplication.java:177) ... 1 more Caused by: java.lang.Illegalargumentexception: Invalid URL: Invalid URL or Resource not found at javafx.scene.image.Image.validateUrl(Image.java:1118) at javafx.scene.image.Image.(Image.java:620) at application. Login.(Login.java:24) ... 13 more Caused by: java.lang.Illegalargumentexception: Invalid URL or Resource not found at javafx.scene.image.Image.validateUrl(Image.java:1110) ... 15 more Exception running application application. Login

The files of the program I’m developing are arranged as follows:

inserir a descrição da imagem aqui

Does anyone know why this mistake occurs? Thank you.

1 answer

0

It’s been a while since I used Javafx, but from what I’m seeing the problem is in the image path, "src/Icons/message.png"

It is looking for the src/Icons folder from the relative class path try using FXML file similar to ".. /Icons/message.png"

  • I got. I changed the code to the following: public Static final String ICON_IMAGE_LOC = "/icons/message.png"; primaryStage.getIcons(). add(new javafx.scene.image.Image(ICON_IMAGE_LOC));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.