Change Icon JavaFx

1

I'm trying to change the icon but I'm not getting it.

Code:

package olamundojavafx;

import com.sun.javafx.scene.SceneHelper;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class OlaMundoJavaFX extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setResizable(false);
        stage.setTitle("Teste");
        SceneHelper.getSceneAccessor();
        stage.show();

        Image image = new Image("/Icons/icon.png");

        stage.getIcons().add(image);

    }

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

}

Error message:

Executing C:\Users\Lucas\Documents\NetBeansProjects\OlaMundoJavaFX\dist\run181984282\OlaMundoJavaFX.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
jan 28, 2018 2:54:21 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 9.0.1 by JavaFX runtime of version 8.0.111
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
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.<init>(Image.java:620)
    at olamundojavafx.OlaMundoJavaFX.start(OlaMundoJavaFX.java:25)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1110)
    ... 11 more
Exception running application olamundojavafx.OlaMundoJavaFX
Java Result: 1
    
asked by anonymous 28.01.2018 / 19:29

1 answer

1

There are two things that draw your attention to your stacktrace:

  

WARNING: Loading FXML document with JavaFX API of version 9.0.1 by   JavaFX runtime of version 8.0.111

It seems like your code and compiler are in different versions

  

Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid   URL or resource not found

In this case he is not finding the resource, you can try this:

stage.getIcons().add(new Image(getClass().getResourceAsStream("/com/seuprograma/icons/icon.png")));

And check to see if the path is spelled correctly.

    
28.01.2018 / 21:41