Cannot load image in JavaFX
Asked Answered
T

6

19

I tested this code in order to create dialog with image.

final int xSize = 400;
final int ySize = 280;
final Color backgroundColor = Color.WHITE;
final String text = "SQL Browser";
final String version = "Product Version: 1.0";

final Stage aboutDialog = new Stage();
aboutDialog.initModality(Modality.WINDOW_MODAL);

Button closeButton = new Button("Close");

closeButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent arg0) {
        aboutDialog.close();
    }
});

GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));

Image img = new Image("logo.png");
ImageView imgView = new ImageView(img);

grid.add(imgView, 0, 0);

grid.add(new Text(text), 0, 1);
grid.add(new Text(version), 0, 2);
grid.add(closeButton, 14, 18);

Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor);
aboutDialog.setScene(aboutDialogScene);
aboutDialog.show();

I placed the image file into the directory /src. But for some reason the image is not displayed. Can you help me to correct my mistake?

Testa answered 19/4, 2013 at 7:25 Comment(3)
possible duplicate of Where does javafx.scene.image.Image("flower.png") look for flower.png?Hysteric
Do you see any problems into the code?Testa
Shouldn't the path be "/src/logo.png" if it is in the src folder?Talithatalk
B
58

Simply replace this code:

Image img = new Image("logo.png");

with this

Image img = new Image("file:logo.png");

Docu reference. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html

When you pass a String to the Image class it can be handled in four different ways (copied from docu):

// The image is located in default package of the classpath
Image image1 = new Image("/flower.png");

// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png");

// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png");

// The image is located in the current working directory
Image image4 = new Image("file:flower.png");

The file: prefix is simply an URI scheme, or in other words the counterpart to the http: protocol classifier. This also works in the file browser, or in the web browser... ;)

For further reference, you can take a look at the wiki page of the file URI scheme: https://en.wikipedia.org/wiki/File_URI_scheme

Happy Coding,

Kalasch

Burress answered 19/4, 2013 at 11:56 Comment(5)
What does the "file:" part do?Microelectronics
I have edited my answer, now I hope that its clear what it does. :)Burress
Strange that Schildt doesn't explain this in Java: The Complete ReferenceRadiometeorograph
I use Image(url); to load 1MB size image, it cost 5 second, too long time! Is there any method to solve this problem?Hootenanny
@LeeRQ: What type of URL is it? HTTP or local file? I haven't had this issue by now.Burress
P
16

Try this:

img = new Image("/logo.png");

If no protocol part indicating a URL (as http: or file:) is given, the file is supposed to reside in the default package. If you want it to put in a different package say com.my.images you add this information in a path like manner:

img = new Image("/com/my/images/logo.png");
Poignant answered 20/4, 2013 at 15:48 Comment(0)
L
9
Image img = new Image("file:/logo.png");

or way with path:

Image img = new Image("file:c:/logo.png");

or

File f = new File("c:\\logo.png");
Image img = new Image(f.toURI().toString());

also can use:

new Image(file:src/logo.png) //root of project
Longdistance answered 15/6, 2016 at 0:25 Comment(0)
P
4

This functions:

Image image  = new Image(getClass()
        .getResourceAsStream("ChimpHumanHand.jpg"));
Pushover answered 7/3, 2015 at 15:45 Comment(1)
To get an image in an arbitrary directory:Pushover
M
0

copy and paste the image into folder where source package(source packages in NetBeans IDE) is present. Then

Image image = new Image("a1.jpg");
Image image = new Image("File:a1.jpg");

both will work.

Meryl answered 22/5, 2015 at 11:58 Comment(0)
I
0

put it in the resources package of project if you are using intellij

 Image icon = new Image("cate_charger.jpg");

Interfluent answered 28/8, 2023 at 8:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.