JavaFX : How to get column and row index in gridpane?

前端 未结 2 1213
长发绾君心
长发绾君心 2020-12-07 04:01

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

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

I searched this. gri

相关标签:
2条回答
  • 2020-12-07 04:40

    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.

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

    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.

    0 讨论(0)
  • 2020-12-07 05:00

    As @Roland points out, you can also assign a listener to each node. When doing so you can use a single callback method with this approach (mouse-pressed handler as example, but works with others too):

    Assigning the handler to the nodes. Let's assume you have an array of buttons:

    for (Button button : buttons) {
        button.setOnMousePressed(this::onPress);
    }
    

    Mouse pressed handler method:

    void onPress(MouseEvent event) {
        int column = GridPane.getColumnIndex((Node) event.getSource());
        int row = GridPane.getRowIndex((Node) event.getSource());
        System.out.println(String.format("Node clicked at: column=%d, row=%d", column, row));
    }
    
    0 讨论(0)
提交回复
热议问题