JavaFX - get index row and index col by OnClick on

2020-04-18 08:10发布

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

0条回答
登录 后发表回答