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
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))); } } |