Peter Penzov Peter Penzov -5 years ago 156
Java Question

Cannot load image in JavaFX

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();

Button closeButton = new Button("Close");

closeButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent arg0) {

GridPane grid = new GridPane();
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);

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

Answer Source

Simply replace this code:

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

with this

Image img = new Image("file:logo.png");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download