0

My icon image will not load for this javafx scene, because it cant find the path, but I cannot see why the path cant be found.

Image of folder layout: Image of folder layout

package com.example.javaassignmentyear2;
 
import javafx.application.Application; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.image.Image; import
javafx.stage.Stage;
 
import java.io.IOException; import java.sql.SQLException;
 
public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("db-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 600, 400);
        Image icon = new Image(getClass().getResourceAsStream("icon.png"));
        stage.getIcons().add(icon);
        stage.setTitle("Game of Thrones: Table View");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) throws SQLException {

        launch();
    }


}
kleopatra
  • 51,061
  • 28
  • 99
  • 211

1 Answers1

0

The icon is a resource, it belongs under resources as James_D has pointed out in a comment. The folder with your Java source files is not on the classpath at runtime, and your build tools are not copying files from the Java source folders into any folder that is on the classpath.

The resources branch of the project layout is where you put files that you need at runtime. They will be included in your .jar file, or placed on the classpath by your IDE while debugging or running your code. This is the same as the .fxml files which are loaded at runtime in the same manner as the icon.

swpalmer
  • 3,890
  • 2
  • 23
  • 31