Elias Jiang
Elias Jiang

Reputation: 21

Problems on JavaFx:The image doesn't show

I inserted an image object in my code. The program compiles and runs, but the image doesn't show.

window is completely white except for title bar

Here is my code

package JavaFX;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.File;

public class ShowImage extends Application {
    public void start(Stage primaryStage){
        Pane pane = new HBox(10);
        pane.setPadding(new Insets(5,5,5,5));
        Image image = new Image(new File("image/v2-7c37a26d9dd77abf6de2ca9ca3fc7ae0_720w.jpg").toURI().toString());//remember this must be an url address,
        // file should be converted to url

        pane.getChildren().add(new ImageView(image));

        ImageView imageView2 = new ImageView(image);
        imageView2.setFitHeight(100);
        imageView2.setFitWidth(100);
        pane.getChildren().add(imageView2);

        ImageView imageView3 = new ImageView(image);
        imageView3.setRotate(90);
        pane.getChildren().add(imageView3);

        Scene scene = new Scene(pane);
        primaryStage.setTitle("ShowImage");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The compiler doesn't show any errors or warnings.

Upvotes: 2

Views: 109

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

Your code generally works, so the problem is most likely an issue with the image.

You should inspect the errorProperty and exceptionProperty, for example...

if (image.errorProperty().getValue()) {
    image.exceptionProperty().getValue().printStackTrace();
}

Runnable example...

import java.io.File;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class App extends Application {

    @Override
    public void start(Stage stage) {
        Pane pane = new HBox(10);
        pane.setPadding(new Insets(5, 5, 5, 5));

        Image image = new Image(new File("image/v2-7c37a26d9dd77abf6de2ca9ca3fc7ae0_720w.jpg").toURI().toString());//remember this must be an url address,
        if (image.errorProperty().getValue()) {
            image.exceptionProperty().getValue().printStackTrace();
        }

        pane.getChildren().add(new ImageView(image));

        ImageView imageView2 = new ImageView(image);
        imageView2.setFitHeight(100);
        imageView2.setFitWidth(100);
        pane.getChildren().add(imageView2);

        ImageView imageView3 = new ImageView(image);
        imageView3.setRotate(90);
        pane.getChildren().add(imageView3);

        Scene scene = new Scene(pane);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

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

Upvotes: 2

Related Questions