Forum Stats

  • 3,826,734 Users
  • 2,260,699 Discussions
  • 7,897,065 Comments

Discussions

Not able to load images from internet ImageView in Javafx

User_Y3BAE
User_Y3BAE Member Posts: 1 Green Ribbon

Here i am trying to load images from url link from internet into ImageView in JavaFx.

Below i show some links.

    String i1 = "https://i.postimg.cc/XvDwHzBc/wide-screen-header-image-2.jpg";
    String i2 = "http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg";
    String i3 = "https://postimg.cc/WDgFtSnh";

To load the images from the internet i am using the code

Image image = new Image(i3,true);

    if (image.isError()){
        System.out.println(image.getException()); //Location 1
    }
    if (image.isBackgroundLoading()){
        System.out.println("Background loading."); //Location 2
    }
    image.progressProperty().addListener(new ChangeListener<Number>() {
        @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
            System.out.println(newValue); //Location 3
        }
    });
    image.errorProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            System.out.println(newValue);  //Location 4
        }
    });

    ImageView imageView = new ImageView();
    imageView.setFitHeight(100.0);
    imageView.setFitWidth(100.0);
    imageView.setImage(image);
    
    StackPane root = new StackPane();
    root.getChildren().add(imageView);
    Scene scene = new Scene(root, 600, 350);
    primaryStage.setTitle("JavaFX Open a new Window.");
    primaryStage.setScene(scene);
    primaryStage.show();

The console output for Location 1,2,3,4 is as follows

this showing image loading works but the image is not displayed in window

what could be the problem kindly help.

Tagged: