0

I've searched the stackoverflow but no solutions are working for my code.

The image for some reason isn't rendering. There is no Icon for the Stage

I tried this: JavaFX Image not showing in stage but the image still wouldn't render. Code:

package com.example.javafxfirst;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.scene.paint.Color;

import java.io.File;

public class HelloApplication extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {

        //Stage stage = new Stage();
        Group root = new Group();
        Scene scene = new Scene(root,Color.BLACK);

        Image icon = new Image(new File("resources/javafxlogo.png").toURI().toString());
        stage.getIcons().add(icon);
        stage.setTitle("Program");
        stage.setScene(scene);
        stage.show();
    }
}

Project Structure enter image description here

Update:

I replaced

Image icon = new Image(new File("resources/javafxlogo.png").toURI().toString());

with

Image icon = new Image(getClass().getResource("/img/javafxlogo.png"));

Now I get the error:

java: no suitable constructor found for Image(java.net.URL)
    constructor javafx.scene.image.Image.Image(java.lang.String) is not applicable
      (argument mismatch; java.net.URL cannot be converted to java.lang.String)
    constructor javafx.scene.image.Image.Image(java.io.InputStream) is not applicable
      (argument mismatch; java.net.URL cannot be converted to java.io.InputStream)
    constructor javafx.scene.image.Image.Image(javafx.scene.image.PixelBuffer) is not applicable
      (argument mismatch; java.net.URL cannot be converted to javafx.scene.image.PixelBuffer)
    constructor javafx.scene.image.Image.Image(java.lang.Object) is not applicable
      (Image(java.lang.Object) has private access in javafx.scene.image.Image)
coderoftheday
  • 1,987
  • 4
  • 7
  • 21
  • The `Image` constructor wants a URL, not a filename. See the duplicate, or https://edencoding.com/where-to-put-resource-files-in-javafx/ – James_D Jan 14 '22 at 21:32
  • @James_D unfortunately that still didn't work. – coderoftheday Jan 14 '22 at 21:38
  • Update your question to show what you tried, along with the results of following the “troubleshooting” section in the linked question – James_D Jan 14 '22 at 21:39
  • Right at the top of the answer to the question I linked, it says *”For images, call `toExternalForm()` on the `URL` to generate the `String` to pass to the `Image` constructor.”*. And then, *”To troubleshoot, examine the contents of your **build** folder, not your **source** folder.”*. Maybe try those things, instead of repeatedly spamming this site with copies of the same question. – James_D Jan 15 '22 at 02:21

0 Answers0