Djoker
Djoker

Reputation: 13

Image does not display in JavaFx

I wanted to create an app like 4 pics 1 word but the image does not display. I used SceneBuilder too. I inserted the image in SceneBuilder and the preview was fine, but when I run the main file it does not show. I tried using a pane and adding the ImageView using .getChildren() and that worked but I couldn't include the FXML file. So any help will be appreciated, Thank You.

Screenshot of the program when I run the main file

Screenshot of the preview from SceneBuilder

public class Main extends Application {

@FXML
private ImageView apple;
//Window
@Override
public void start(Stage primaryStage) throws Exception{
    try
    {
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        Scene scene = new Scene(root, 546, 600);
        primaryStage.setTitle("4 Pics 1 Word");
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    //Time display


}


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

EDIT 1:FXML code for ImageView:

<ImageView fx:id="apple" fitHeight="360.0" fitWidth="430.0" layoutX="53.0" layoutY="30.0" pickOnBounds="true">
     <image>
        <Image url="@../../Apple.jpg" />
     </image>
  </ImageView>

Upvotes: 1

Views: 2256

Answers (1)

Damien
Damien

Reputation: 1209

I found that to display pictures in your JavaFX application from a fxml file, you have to precede the picture name with file: and use a relative path, such as:

file:Apple.jpg

in the Image field of the "Specific" ImageView section in SceneBuilder.

This will display the image in your application but will replace it with a question mark (image not found) in the SceneBuilder screen.

Upvotes: 0

Related Questions