GridPane col/row coordinate without node

131 Views Asked by At

I'm trying to figure out where the mouse is, in terms of a GridPane's column and row. I know the mouse position, so I could presumably figure it out from that, if I knew each cell's width and height (which I don't see an easy way to figure out) but most of the code I've seen seems to presume the mouse is over a node inside the GridPane:

        Node node = (Node) event.getTarget();
        int row = GridPane.getRowIndex(node);
        int column = GridPane.getColumnIndex(node);

But I actually don't have anything inside my GridPane, so I just get the whole GridPane. Now, I suppose I could add nodes for each of the cells, but this seems inefficient. (The idea is that the user is placing the nodes in a grid, so the grid starts out empty.)

Edit: To clarify, I basically want to do what SceneBuilder does when you place a GridPane down and then drag a component over it. The cell you're hovering over is highlighted and then (if you drop the component) it gets added to that cell. So I know it's possible (and also not outlandish as a concept).

Any thoughts?

1

There are 1 best solutions below

0
On

This doesn't exactly answer my question but it's useful for me short term and it might be useful for others. It works only when the cells are uniform across the dimension being queried (i.e., all the same width/height).

 public int getCoord(double width, double coord, int numberOfSections) {
        long dim = Math.round(width / numberOfSections);
        long border = dim;
        int val = 0;
        while(coord > border) {
            border += dim;
            val++;
        }
        return val;
    }

Lacking a way to directly iterate over the current state of rows and columns, I think the full solution requires an analysis of children (if there are any) and the row and column constraints.