How to get the coordinates of the element in the gridpane on the element itself? Something like the grid. Get the coordinate X (object) or something more cunning

    1 answer 1

    Especially not tested, but for standard situations it seems to work:

    import javafx.application.Application; import javafx.geometry.Bounds; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.stage.Stage; import javafx.util.Pair; import java.util.Arrays; public class Main extends Application { /** * ВсС элСмСнты ΠΎΠ΄Π½ΠΎΠΉ строки ΠΈΠΌΠ΅ΡŽΡ‚ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠ΅ свойство minY. Π—Π½Π°Ρ‡ΠΈΡ‚ ΠΊΠ°ΠΆΠ΄ΠΎΠ΅ ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ соотвСтсвуСт * ΠΎΠ΄Π½ΠΎΠΉ строкС. * @param gridPane Π˜ΡΡ…ΠΎΠ΄Π½Π°Ρ панСль * @return ΠžΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΏΠΎ Π²ΠΎΠ·Ρ€Π°ΡΡ‚Π°Π½ΠΈΡŽ массив Y ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ строк. */ private static double[] getRowsY(final GridPane gridPane) { return gridPane.getChildren().stream() .mapToDouble(node -> node.getBoundsInParent().getMinY()) .distinct().sorted().toArray(); } /** * Π’ Ρ€Π°ΠΌΠΊΠ°Ρ… ΠΎΠ΄Π½ΠΎΠΉ строки, ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ элСмСнт ΠΈΠΌΠ΅Π΅Ρ‚ ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½ΠΎΠ΅ свойство minX. * @param gridPane Π˜ΡΡ…ΠΎΠ΄Π½Π°Ρ панСль * @param rowMinY ΠšΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π° minY строки, для ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ minX элСмСнтов * @return ΠžΡ‚ΡΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΠΏΠΎ Π²ΠΎΠ·Ρ€Π°ΡΡ‚Π°Π½ΠΈΡŽ массив X ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ элСмСнтов строки. */ private static double[] getColumnsX(final GridPane gridPane, double rowMinY) { return gridPane.getChildren().stream() .map(node -> node.getBoundsInParent()) .filter(bounds -> bounds.getMinY() == rowMinY) .mapToDouble(bounds -> bounds.getMinX()) .distinct().sorted().toArray(); } /** * ΠŸΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ элСмСнта * @param gridPane Π˜ΡΡ…ΠΎΠ΄Π½Π°Ρ панСль * @param node Π˜ΡΠΊΠΎΠΌΡ‹ΠΉ элСмСнт * @return ΠŸΠ°Ρ€Π° (rowIndex, columnIndex) элСмСнта */ public static Pair<Integer, Integer> getPosition(final GridPane gridPane, final Node node) { if (!gridPane.getChildren().contains(node)) { return null; } Bounds boundsInParent = node.getBoundsInParent(); double[] rowsY = getRowsY(gridPane); int rowIndex = Arrays.binarySearch(rowsY, boundsInParent.getMinY()); double[] columnsX = getColumnsX(gridPane, boundsInParent.getMinY()); int columnIndex = Arrays.binarySearch(columnsX, boundsInParent.getMinX()); return new Pair<>(rowIndex, columnIndex); } public void start(Stage primaryStage) { GridPane gridPane = new GridPane(); final Label searched = new Label("1-2"); final Label fail = new Label("Fail"); gridPane.addRow(0, new Label("0-0"), new Label("0-1"), new Label("0-2")); gridPane.addRow(1, new Label("1-0"), new Label("1-1"), searched); gridPane.addRow(2, new Label("2-0"), new Label("2-1"), new Label("2-2")); primaryStage.setScene(new Scene(gridPane, 100, 100)); primaryStage.show(); System.out.println(getPosition(gridPane, fail)); System.out.println(getPosition(gridPane, searched)); System.out.println(getPosition(gridPane, gridPane.getChildren().get(6))); } }