I’m coding with JavaFXML
and Gluon Scene Builder 8.0.0
to create a pixel editor app. I have created two .fxml files, one for drawing tools (sample.fxml
) and the other for an array of Circle Objects (32 x 32) that represents a pixel array of LEDs (PixelEditor.fxml
). Both share the same controller (Controller.java
).
I can’t initialize my Circle[][]
array in Controller.java
when the user clicks on a menu item like 32h x 32w
. I used a 4 x4 array to test my code:
public void handleMenuAction(ActionEvent event) throws IOException {
if(event.getSource() == menu32hx32w) {
Stage pixelStage = new Stage();
Parent pixelRoot = FXMLLoader.load(getClass().getResource("PixelEditor.fxml"));
Scene pixelScene = new Scene(pixelRoot);
pixelStage.setTitle("Pixel Array: 32h X 32w");
pixelStage.setScene(pixelScene);
pixelStage.setX(0.0);
pixelStage.setY(0.0);
pixelStage.show();
Circle[][] pixelArray = {
{R0C0, R0C1, R0C2, R0C3},
{R1C0, R1C1, R1C2, R1C3},
{R2C0, R2C1, R2C2, R2C3},
{R3C0, R3C1, R3C2, R3C3},
};
}
}
If I print out the array I get:
pixelArray:
null null null null
null null null null
null null null null
null null null null
When I had only one .fxml containing all the Objects I could initialize the pixelArray. I use fx:id to reference the Circle Objects but placing them in a separate Stage and Scene seems to de-reference them and create a null elements.
What am I not doing?
Previously, with one .fxml file, all I needed to assign values to the Circle Objects was to reference their fx:id in the Controller.java as follows:
@FXML
private Circle
R0C0, R0C1, R0C2, R0C3,
R1C0, R1C1, R1C2, R1C3,
R2C0, R2C1, R2C2, R2C3,
R3C0, R3C1, R3C2, R3C3;
This is what I'm still doing but the assigned properties via the fx:id reference don't seem to connect?
The PixelEditor.fxml is quite large because I have 32x32 = 1024 Circle Objects even though I'm just testing with the first 4x4. The code for the first row looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane fx:id="panePixelLayout" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="776.0" prefWidth="776.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox prefHeight="776.0" prefWidth="776.0" style="-fx-background-color: #000000;">
<children>
<HBox prefHeight="24.0" prefWidth="776.0" style="-fx-background-color: #000000;">
<children>
<Circle fx:id="R0C0" fill="DODGERBLUE" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
<Circle fx:id="R0C1" fill="DODGERBLUE" layoutX="22.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
<Circle fx:id="R0C2" fill="DODGERBLUE" layoutX="22.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>
<Circle fx:id="R0C3" fill="DODGERBLUE" layoutX="42.0" layoutY="22.0" onDragDetected="#onDragDetected" onMouseClicked="#pixelClicked" onMouseDragEntered="#onMouseDragEntered" onMousePressed="#onMousePressed" radius="8.0" stroke="BLACK" strokeType="INSIDE" style="-fx-fill: DARKGREY;">
<HBox.margin>
<Insets left="8.0" top="8.0" />
</HBox.margin>
</Circle>