This question already has an answer here:
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