1

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>
sgroen
  • 29
  • 1
  • 8
  • Take a moment to read through the [editing help](http://stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than other sites. The better your post looks, the easier it will be for users to help you. – gunr2171 Sep 01 '15 at 19:14
  • Can you [edit] to include your `PixelEditor.fxml` file please? – durron597 Sep 01 '15 at 20:40

2 Answers2

0

It looks like you haven't actually set values to any of: R0C0, R0C1, etc. If these variables are set to null when you create the array, they will still be null even if you set them later.

You haven't shown the part of your code where you assign R0C0, but most likely, that is where the problem is.

durron597
  • 31,968
  • 17
  • 99
  • 158
  • 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: – sgroen Sep 01 '15 at 20:19
  • @sgroen [Edit](http://stackoverflow.com/posts/32339237/edit) that information into your question, don't post it in the comments, please. – durron597 Sep 01 '15 at 20:21
0

This question was answered in a related question by jewelsea who provided a most excellent answer at: Have multiple FXML files (created in SceneBuilder), but only one controller. Does each scene load it's own copy of the controller?.

By creating two controllers, I was able to initialize my Circle[][] array but now I will have to pass parameters between two controllers as jewelsea describes via links provided.

Community
  • 1
  • 1
sgroen
  • 29
  • 1
  • 8