Reputation: 1606
I'm using the SceneBuilder to design an interface and used the code here to set up a simple FileChooser to open and view picture files.
The code isolated was tested and works fine, I tried to integrate it into my existing interface and it just keeps failing. No error message, nothing.
I have started putting println
around the commands and everything "works".
Here's my GUI, the design works so far (I just started using a VBox
instead of a regular Panel
as all my attempts had failed)
<HBox prefHeight="790.0" prefWidth="927.0" style="-fx-background-color: green;">
<children>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<ImageView fx:id="iv_main" fitHeight="473.0" fitWidth="462.0" pickOnBounds="true" preserveRatio="true" />
</children>
</VBox>
<VBox prefHeight="790.0" prefWidth="236.0">
<!-- And things inside -->
</VBox>
</children>
</HBox>
And the code in the controller - the initializer doesn't display any picture in that version here. Yet if I do the same in the original file it works:
@FXML
private ImageView iv_main;
//I know this here looks nasty but I tried the path 1:1 in the
//isolated version (even a different project) - but works 100%
private String RESJPG = "/home/qohelet/NetBeansProjects/KV/src/main/resources/fxml/ahok.jpg";
@Override
public void initialize(URL url, ResourceBundle rb) {
/*
* In the original file here are two ObservableLists
* filled successfully. So I can assume the initialize-method works
*/
btn_openFolder.setOnAction(btnLoadEventListener);
try {
FileInputStream input = new FileInputStream(RESJPG);
Image image = new Image(input);
iv_main = new ImageView(image);
} catch (FileNotFoundException|NullPointerException | java.lang.IllegalArgumentException nupo) {
System.err.println(RESJPG + " not found");
}
}
The event-handler works perfectly fine on that action event - yet it does nothing to the ImageView:
EventHandler<ActionEvent> btnLoadEventListener
= new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
System.out.println("Click");
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("Bilddateien", "*.JPG", "*.jpg", "*.jpeg", "*.JPEG", "*.PNG", "*.png");
fileChooser.getExtensionFilters().addAll(extFilterJPG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
System.out.println("File: " + file.getAbsolutePath());
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
iv_main.setImage(image);
iv_main.setVisible(true); //In Swing you sometimes had to do that command - but here regardless if inserted or not, no changes
} catch (IOException ex) {
Logger.getLogger(FXMLController.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
I'm running out of ideas now... The file is correctly displayed in the console, I keep clean and building the project, moved the ImageView around so there's no chance it's just covered with another object, but there's no changes in the result...
I'm even trying those two ways to change the image (once on start, then with the FileChooser
) - yet nothing.
What am I doing wrong?
(Related to the version (com.sun.javafx.runtime.VersionInfo.getRuntimeVersion()
), I'm using: 8.0.131-b11 )
Upvotes: 1
Views: 1320
Reputation: 209330
Never initialize fields that are annotated @FXML
. You are changing the image of the ImageView
you create in the initialize()
method, instead of the one created in the FXML file. Obviously, the one created in the FXML file is the one that is actually displayed in the UI, and the one you create in the initialize()
method is never displayed (you never put it in the scene graph anywhere); so you are changing the image of an image view that is not displayed.
Change
iv_main = new ImageView(image);
to
iv_main.setImage(image);
Upvotes: 2