JavaFX : How to get column and row index in gridpane?

前端 未结 2 1219
长发绾君心
长发绾君心 2020-12-07 04:01

I tried to get the row/column index of gridpane, when I click.

I am looking for method like as getSelectedColumn() in JTable(java swing)

I searched this. gri

2条回答
  •  无人及你
    2020-12-07 05:00

    As @Roland points out, you can also assign a listener to each node. When doing so you can use a single callback method with this approach (mouse-pressed handler as example, but works with others too):

    Assigning the handler to the nodes. Let's assume you have an array of buttons:

    for (Button button : buttons) {
        button.setOnMousePressed(this::onPress);
    }
    

    Mouse pressed handler method:

    void onPress(MouseEvent event) {
        int column = GridPane.getColumnIndex((Node) event.getSource());
        int row = GridPane.getRowIndex((Node) event.getSource());
        System.out.println(String.format("Node clicked at: column=%d, row=%d", column, row));
    }
    

提交回复
热议问题