JavaFX ImageView via FXML not working

I have a problem loading images from ImageView to FXML.

My controller class:

 public class BoxViewController { @FXML private Label label_boxID; @FXML private ImageView boximage; public void initData(ObservableList<BoxProperty> observableList, BoxService sBox, TableView tableview) { this.label_boxID.setText( String.valueOf(this.boxproperty.getPboxid())); Image image = new Image("boximage.jpg"); this.boximage = new ImageView(); this.boximage.setImage(image); } } 

So, setting a shortcut with text works, but the image will not appear in my ImageView. For ImageView, I added an ID to the FXML file:

  <ImageView fx:id="boximage" disable="false" fitHeight="150.0" fitWidth="200.0" layoutX="69.0" layoutY="322.0" pickOnBounds="true" preserveRatio="true" /> 

I am confused why this does not work, because the shortcut works, but the image does not load.
I also checked if boximage nonempty, but it is not. There are also no exceptions.

+6
source share
2 answers

It works:

 BufferedImage bufferedImage; bufferedImage = ImageIO.read(new File(this.path)); Image image = SwingFXUtils.toFXImage(bufferedImage, null); this.boximage.setImage(image); 
+4
source

Perhaps this is a problem with the placement of the original image. According to the comments on this website from "Maxim" if you are using new Image("boximage.jpg"); , the root directory is the main folder of the project and, for example, for scene.getStylesheets().add("login.css"); src root folder Perhaps you could try this:

 Image img = new Image("file:boximage.jpg"); ImageView imageView = new ImageView(img); 

Try moving the original image to the main project folder for this code.

Just for testing, try downloading this image from FXML :

 <ImageView id="boxImage" ...> <image> <Image url="@boximage.jpg" /> </image> </ImageView> 
+8
source

All Articles