All the functionalities are working fine except this image display. But in Scene builder preview its working fine. Can someone help in this??
Inside Scene Builder, drag an ImageView on top of a Label and it will automatically be set as a graphic for the label. Then select the ImageView and type the url of the image into the ImageView's image field in the Scene Builder properties pane.
You can load and modify images using the classes provided by JavaFX in the package javafx. scene. image. JavaFX supports the image formats like Bmp, Gif, Jpeg, Png.
Create a FileInputStream representing the image you want to load. Instantiate the Image class bypassing the input stream object created above, as a parameter to its constructor. Instantiate the ImageView class. Set the image to it by passing above the image object as a parameter to the setImage() method.
maybe you linked images from outside the projectdirectory, i made a small and simple example, which works quite well.
Put your image into the same package, where you placed your fxml file and link it again in Scene Builder to the new location.
Here's a little code: App.java
package com.example.images;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application{
@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("images.fxml"));
stage.setTitle("Image set in Scene Builder");
Scene scene = new Scene(parent);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
And the fxml File:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.example.images.controller.MainController">
<children>
<ImageView fitHeight="337.875" fitWidth="540.6" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<effect>
<Lighting surfaceScale="5.0">
<bumpInput>
<Shadow />
</bumpInput>
<light>
<javafx.scene.effect.Light.Distant azimuth="-135.0" />
</light>
</Lighting>
</effect>
<image>
<Image url="@1.png" backgroundLoading="true" />
</image>
</ImageView>
</children>
</AnchorPane>
Patrick
i was using intelliJ and had the exact problem twice, here's how to deal with it:
one time the problem was that the images were not in a package (i.e. there were in src with no package) and as soon as i made a package and moved pictures there , the images were loaded.
the other time the problem was solved by deleting the "out" directory of intelliJ and building the project again.
I have faced two cases for this problem. One is that maybe you don't have your images in a "recources" folder in your "src" directory. Two, if you do, then probably your IDE just has not read it yet. For example if you're using Eclipse, expand your recources folder on File Explorer. If your image is not in there then right click on the folder and select refresh. You should be fine after that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With