how get column index and row index in gridpane of javafx
Asked Answered
C

2

8

how get column index and row index in GridPane of JavaFX. see the code below

Text text1 = new Text("Text 1");
Text text2 = new Text("Text 2");
StackPane root = new StackPane();
GridPane gridPane = new GridPane();
gridPane.add(text1, 0, 0);
gridPane.add(text2, 1, 0);

When Mouse Entered On text1 I want to get the column index and row index of GridPane

text1.setOnMouseEntered(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent e) {
        //want to get column index =0 and row index=0
    }
});

Please let me know.

Cynthea answered 13/6, 2013 at 9:45 Comment(0)
T
10

You can get the row index and column index by utilising the static methods getRowIndex() and getColumnIndex() which are located in the GridPane class.

System.out.println("Row: " + GridPane.getRowIndex(text1));
System.out.println("Column: " + GridPane.getColumnIndex(text1));

See for the reference.

Talkative answered 14/6, 2013 at 6:45 Comment(2)
can i get index without getting add any node on grid paneCynthea
The method GridPane.getColumnIndex(text1) or GridPane.getRowIndex(text1) will not add any node on the GridPane. It just returns the index of the passed item.Murther
R
-1

When i was looking for the same answer as OP I searched a lot and eventually found this.

GridPane.getColumnIndex((Node) e.getTarget());

GridPane.getRowIndex((Node) e.getTarget());

It works to get the index in an Integer format by clicking on the Node you want the coordinates from if you are using a GridPane to make your "Grid"

Readability answered 10/12, 2020 at 12:1 Comment(1)
How is your answer any more useful than that of @ShreyasDave?Induration

© 2022 - 2024 — McMap. All rights reserved.