Home > Software engineering >  Problems on JavaFx:The image doesn't show
Problems on JavaFx:The image doesn't show

Time:05-24

I insert an image object in my code,the program could run,but the image doesn't show.

enter image description here Image path is correct? Check this pic.

CodePudding user response:

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();
    }
}
  • Related