Reputation: 233
I'm having a problem getting my ImageView
component injected through FXMl in JavaFX. I have a controller written in Scala as so:
package me.mycontroller
import javafx.fxml.FXML
import java.util.ResourceBundle
import java.net.URL
import javafx.scene.image.ImageView
/**
* @author me
*/
class InvoiceController {
@FXML
private var resources: ResourceBundle = null
@FXML
private var location: URL = null
@FXML
private var imageBox2: ImageView = null
@FXML
def initialize() {
if(imageBox2==null) { throw new IllegalArgumentException("imageBox was null")}
}
}
and my FXMl is like so:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="286.0" prefWidth="512.0" xmlns:fx="http://javafx.com/fxml" fx:controller="me.mycontroller.InvoiceController">
<children>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="435.0" spacing="10.0" AnchorPane.bottomAnchor="58.0" AnchorPane.leftAnchor="38.5" AnchorPane.rightAnchor="38.5" AnchorPane.topAnchor="128.0">
<children>
<Label text="Invoice File" />
<TextField fx:id="fileField" prefWidth="200.0" />
</children>
</HBox>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="400.5" spacing="10.0" AnchorPane.bottomAnchor="-7.0" AnchorPane.leftAnchor="52.5" AnchorPane.rightAnchor="59.0" AnchorPane.topAnchor="193.0">
<children>
<AnchorPane prefHeight="100.0" prefWidth="376.0">
<children>
<AnchorPane layoutX="16.0" layoutY="0.0" prefHeight="100.0" prefWidth="294.0">
<children>
<Label layoutY="42.0" prefWidth="65.0" text="Supplier" AnchorPane.leftAnchor="25.0" />
<ChoiceBox fx:id="supplierDropDown" layoutY="40.0" prefWidth="200.0" AnchorPane.rightAnchor="5.0">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="Item 1" />
<String fx:value="Item 2" />
<String fx:value="Item 3" />
</FXCollections>
</items>
</ChoiceBox>
</children>
</AnchorPane>
</children>
</AnchorPane>
</children>
</HBox>
<ImageView fx:id="imageBox2" fitHeight="105.99999961206467" fitWidth="141.3333282470703" layoutX="53.0" layoutY="22.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/Regional.jpeg" />
</image>
</ImageView>
</children>
</AnchorPane>
When I run the program like:
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Invoice.fxml"));
I get an exception because imageBox2
is null. Any idea why it's not getting injected?
Upvotes: 3
Views: 1920
Reputation: 476
src/ main/ Main.java MainLayoutController.java main_layout.fxml ...
MainLayoutController.java:
public class MainLayoutController {
@FXML
private ImageView previewImage;
void setImage(Image img) {
previewImage.setImage(img);
}
}
main_layout.fxml:
<VBox ... fx:controller="main.MainLayoutController">
<children>
...
<ImageView fx:id="previewImage" ... />
</children>
</VBox>
Main.java:
public class Main extends Application {
private MainLayoutController mainLayoutController;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
Parent root = fxmlLoader.load(
getClass().getResource("main_layout.fxml").openStream());
primaryStage.setScene(new Scene(root));
primaryStage.show();
mainLayoutController = fxmlLoader.getController();
mainLayoutController.setImage(...);
}
...
}
Upvotes: 0