如何访问GridPane中生成的单元格?

时间:2016-06-05 13:26:13

标签: javafx

我需要在GridPane中的特定单元格(中间居中的单元格)中添加一个形状。

这就是我的GridPane中的单元格(StackPane):

<?php

    if (get_post_meta($post->ID,'video_url',true)) ?>
        echo '<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID,'video_url',true);?>/0.jpg"/>';

    // Check if the post has a Post Thumbnail assigned to it
    else ( has_post_thumbnail() ) {
        echo '<a href="' . get_permalink($post->ID) . '" >';
        the_post_thumbnail('frontpage-thumb');
        echo '</a>';

    // If the post does not have a featured image then display the fallback image
    } else {
        echo '<a href="' . get_permalink($post->ID) . '" ><img src="'. get_stylesheet_directory_uri() . '/img/fallback-featured-image-index.jpg" /></a>';}
?>

我试图用这种方法获得居中的细胞:

private StackPane createCell() {
    StackPane cell = new StackPane();
    cell.getStyleClass().add("cell");
    return cell;
}

然后,获取节点:

private Node getCenteredNodeGridPane(GridPane gridPane, int col, int row) {
    for (Node node : gridPane.getChildren()) {
        if (GridPane.getColumnIndex(node) == col/2 && GridPane.getRowIndex(node) == row/2) {
            return node;
        }
    }
    return null;
}

但我无法访问此节点的实际StackPane。 我需要类似的东西,

  

centeredNode.getChildren()添加(形状);

1 个答案:

答案 0 :(得分:1)

假设您只添加StackPane s作为网格窗格的子节点,您只需要转换结果:

StackPane centeredNode = (StackPane) getCenteredNodeGridPane(grid, 20, 20);

根据您的具体要求,您当然可以在getCenteredNodeGridPane方法中执行此操作,并添加类型检查:

private StackPane getCenteredNodeGridPane(GridPane gridPane, int col, int row) {
    for (Node node : gridPane.getChildren()) {
        if (node instanceof StackPane 
         && GridPane.getColumnIndex(node) == col/2 
         && GridPane.getRowIndex(node) == row/2) {
            return (StackPane) node;
        }
    }
    return null;
}