1

I need to get the index of my specified click over my GridPane called myGrid. If I put a piece on board from my coord like here below, it works.. for example: myGrid.add(new ImageView("othello/images/white.png"), 4, 3);

If I want to take the position of my click on board I use this method without success..

@FXML
   private void clickGrid(MouseEvent event) {
      Node source = (Node)event.getSource() ;
      Integer colIndex = GridPane.getColumnIndex(source);
      Integer rowIndex = GridPane.getRowIndex(source);
      if (colIndex != null && rowIndex != null){
         myGrid.add(new ImageView("othello/images/black.png"), colIndex.intValue(), rowIndex.intValue());
      }     
   }

If I don't use

if (colIndex != null && rowIndex != null)

the error is "Java.NullPointException"

However if I use that in the program, Nothing happens when I try to get the row/col values. Help? Thank you

EDIT: here my FXML

enter image description here

Alberto32
  • 229
  • 1
  • 4
  • 14
  • The source of the event is the node with which the handler is registered. Which node(s) is the handler applied to in the FXML file? – James_D Jul 20 '17 at 15:50
  • clickGrid is in "On Action" of my GridPane, in FXML. If I click my GridPane he should give me the right row, col index. – Alberto32 Jul 20 '17 at 16:13
  • No, because `GridPane.getColumnIndex(node)` gives you the column index of the node, which is assumed to be in a `GridPane`. Your `GridPane` probably isn't contained in another `GridPane` (and even if it were, that wouldn't be the column/row index you want). You need to register the mouse listener with the nodes that are inside the `GridPane`, not the `GridPane` itself. – James_D Jul 20 '17 at 16:17
  • See, for example, https://stackoverflow.com/questions/41125571, or https://stackoverflow.com/questions/31095954, or many others – James_D Jul 20 '17 at 16:19
  • 1
    You can use this link https://stackoverflow.com/questions/28320110/javafx-how-to-get-column-and-row-index-in-gridpane – Akash Jul 20 '17 at 16:20
  • Awwww ok. I have understand the logic, but I do lots of confusion. I have edited my post to show you my file FXML, thank you :) – Alberto32 Jul 20 '17 at 16:27

0 Answers0