-1

I am new to Java FXML. I am still learning how to use Scene Builder 2.0 and controller classes. I don't understand why this is returning null

@FXML
public void gridClick(MouseEvent e){
    Node source = (Node)e.getSource() ;
    Integer colIndex = grid.getColumnIndex(source);
    Integer rowIndex = grid.getRowIndex(source);
    System.out.println("Mouse entered cell ["+colIndex+","+rowIndex+"]");
}

I initiated all the grid cells in SceneBuilder The gridpane has been created in SceneBuilder Then I created the anchorpanes for each cell in the controller class initialization:

for(int row=0;row<numRows;row++){
    for(int col=0;col<numCols;col++){
        AnchorPane slot=new AnchorPane();
        slot.setStyle("-fx-border-color: black;-fx-border-width: 0.2;");
        grid.add(slot,col,row);
    }
}

I don't know if this is a bad approach but i am trying to learn. Please help me how to do this differently or why it is giving me null value. When the user clicks on one of the grid cells, i.e. they should be anchorpanes, I want the Anchorpane that they clicked or the grid cell row and column.

Community
  • 1
  • 1
  • I would start by verifying that `source` is in fact an AnchorPane. I would also verify that its parent is a GridPane. – VGR Oct 24 '19 at 01:36
  • Here, the 'grid' is the GridPane. So I thought it will add as a child of 'grid'. And how do I check the type of source? – Tammy Bammy Oct 24 '19 at 01:38
  • I have checked the parent of the AnchorPane identifiers and the `source` and it is `grid`. And it is still returning null. – Tammy Bammy Oct 24 '19 at 01:45
  • this is a bad approach @TammyBammy, you should use `Controllers`, it's much better and easier in JavaFX – Alan Deep Oct 24 '19 at 02:39
  • One thing that I noticed in the past was that Scenebuilder would not put a zero in the FXML. So I would look at the FXML and make sure all zero indexed gridcells had zero's in the FXML. Also make sure you have some type of Node in each cell. Have a look at [this](https://github.com/sedj601/GameOfFifteen) code. – SedJ601 Oct 24 '19 at 03:02
  • 2
    " Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [mre]" – c0der Oct 24 '19 at 14:40

1 Answers1

0

I don't know if this is a problem anymore, but looking for answers I came across this topic, so I will answer it in case someone needs answer. First of all, the problem is with accessing node itself with command: Integer colIndex = grid.getColumnIndex(source);. Command getColumnIndex is static, so you have to access the type directly: GridPane.getColumnIndex(source);

Secondly, you are trying to retrieve coordinates of a grid pane without anything inside the grid pane, which doesn't work because grid pane cell without children, which means node type, is null. There must be a node inside a grid pane intersection, let's say ImageView, meaning all nodes must be cast to ImageView type node:

for(Node i : grid.getChildren){ImageView iv = (ImageView)i;

Now you can retrieve indexes:

int colIndex = GridPane.getColumnIndex(iv);}

Basically, this means that method must know what is the type of node whose indexes it is retrieving.

And lastly, be careful with fxml, because, if you, for example, drag and drop ImageView or button to grid pane in SceneBuilder, if grid pane cell has row or column index 0, fxml will assume default value, which is zero and it will not assign value explicitly:

<example>
<ImageView fx:id="IvTarget_0_0" fitHeight="55.0" fitWidth="58.0" onDragDropped="#handleImageDrop" pickOnBounds="true" preserveRatio="true">
               <GridPane.margin>
                  <Insets left="12.0" />
               </GridPane.margin>
            </ImageView>
</example>

so you see that node with coordinates 0,0 in your fxml editor doesn't have GridPane.columnIndex or GridPane.rowIndex values set. This is OK for other functionalities, but when it comes to retrieving indexes, it will throw nullPtr exception, even if just one of multiple children doesn't have these properties set. In this case, you have to review all nodes in fxml editor and set them manually.

<example>
<ImageView fx:id="IvTarget_0_0" GridPane.columnIndex="0" GridPane.rowIndex="0" fitHeight="55.0" fitWidth="58.0" onDragDropped="#handleImageDrop" pickOnBounds="true" preserveRatio="true">
               <GridPane.margin>
                  <Insets left="12.0" />
               </GridPane.margin>
            </ImageView>
</example>

Hope this helps!