JavaFX:如何在网格窗格中获取列和行索引? [英] JavaFX : How to get column and row index in gridpane?

查看:39
本文介绍了JavaFX:如何在网格窗格中获取列和行索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击时,我试图获取网格窗格的行/列索引.

I tried to get the row/column index of gridpane, when I click.

我正在寻找像 getSelectedColumn() in JTable(java swing)

I am looking for method like as getSelectedColumn() in JTable(java swing)

我搜索了这个.gridPane.getRowIndex(node)

但这对我没有帮助.因为它需要节点名称来获取行/列索引.

But it doesn't help to me. Because it need node name to get row/column index.

我为语法制作了一个网格组件.所以节点名是一样的.我怎样才能获得价值?我的部分代码如下.

I make a component of grid by for grammar. So the node name is same. How can I get the value? My partial code is below.

for (int i = 0; i < imageName.length; i++) {
   try {
      img = new Image(getClass().getResourceAsStream(imageName[i]));   
   } catch (Exception e) {
      System.out.println("exception : " + e);
   }
      ImageView imageview = new ImageView(img);
      imageview.setFitWidth(40);
      imageview.setFitHeight(40);

      HBox hbox= new HBox();
      hbox.getChildren().add(imageview);
      tile1.setHgap(40);
      tile1.setVgap(40);
      tile1.add(hbox, i+2, 0);
}

推荐答案

这里有一个示例,说明如果您只想要网格窗格上的鼠标侦听器而不是单元格中的节点上的鼠标侦听器,如何执行此操作.为简单起见,我使用 Label 作为单元节点,但您可以使用任何您喜欢的.

Here's an example about how you could do it if you want only a mouse listener on the gridpane and not on the nodes in the cells. For simplicity I used a Label as cell node, but you can use whatever you prefer.

public class Demo extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {

        Pane root = new Pane();

        GridPane gridPane = new GridPane();
        gridPane.setGridLinesVisible(true);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {

                Label label = new Label("Label " + i + "/" + j);
                label.setMouseTransparent(true);
                GridPane.setRowIndex(label, i);
                GridPane.setColumnIndex(label, j);

                gridPane.getChildren().add(label);
            }
        }

        root.getChildren().add( gridPane);

        Scene scene = new Scene(root, 400, 300, Color.WHITE);
        primaryStage.setScene(scene);

        primaryStage.show();

        gridPane.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent e) {

                for( Node node: gridPane.getChildren()) {

                    if( node instanceof Label) {
                        if( node.getBoundsInParent().contains(e.getSceneX(),  e.getSceneY())) {
                            System.out.println( "Node: " + node + " at " + GridPane.getRowIndex( node) + "/" + GridPane.getColumnIndex( node));
                        }
                    }
                }
            }
        });

    }

}

您会在控制台中看到点击的单元格信息.

You see the clicked cell information in the console.

如果您将侦听器放在单元格节点而不是网格窗格上,同样会起作用,这里是一个 lambda 表达式:

The same would work if you'd put the listener on the cell node instead of the gridpane, here as a lambda expression:

        label.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> System.out.println( "Node: " + label + " at " + GridPane.getRowIndex( label) + "/" + GridPane.getColumnIndex( label)));

但请注意 getRowIndexgetColumnIndex 方法仅在先前设置数据时才起作用,如这些方法的文档中所述.

But be aware that the getRowIndex and getColumnIndex methods work only if the data were previously set, as specified in the documentation to these methods.

我没有关于您打算实现什么的信息,但我个人更喜欢使用节点本身而不是布局管理器中可能会更改的某些索引.

I have no information about what you intend to achieve, but personally I prefer to work with the nodes themselves instead of some indices in a layout manager which may change.

这篇关于JavaFX:如何在网格窗格中获取列和行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆