Подтвердить что ты не робот

JavaFX: Получить Node по строке и столбцу

Есть ли способ получить конкретный Node из gridPane, если я знаю его местоположение (строку и столбец) или любой другой способ получить Node из gridPane?

4b9b3361

Ответ 1

Я не вижу прямого API для получения индекса столбца node по строкам, но вы можете использовать API getChildren от Pane и getRowIndex(Node child) и getColumnIndex(Node child) от GridPane

//Gets the list of children of this Parent. 
public ObservableList<Node> getChildren() 
//Returns the child column index constraint if set
public static java.lang.Integer getColumnIndex(Node child)
//Returns the child row index constraint if set.
public static java.lang.Integer getRowIndex(Node child)

Вот пример кода для получения Node с использованием индексов строк и столбцов из GridPane

public Node getNodeByRowColumnIndex (final int row, final int column, GridPane gridPane) {
    Node result = null;
    ObservableList<Node> childrens = gridPane.getChildren();

    for (Node node : childrens) {
        if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) {
            result = node;
            break;
        }
    }

    return result;
}

Важное обновление: getRowIndex() и getColumnIndex() теперь являются статическими методами и должны быть изменены на GridPane.getRowIndex(node) и GridPane.getColumnIndex(node).