JavaFX - get index row and index col by OnClick on GridPane [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 05:23:09

问题


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

来源:https://stackoverflow.com/questions/45219540/javafx-get-index-row-and-index-col-by-onclick-on-gridpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!