JavaFX force all cells to be of the same size in a GridPane


Question

I am building a Reversi JavaFX game (modifying the example from Pro JavaFX).

Each cell is a stackpane containing a background and an ellipse representing the piece reversi background and piece

The ellipse radiusX is bound to radiusY (so it's a circle), and radiusY is bound to the min of the parent's width and height:

 DoubleBinding parentWidth = Bindings.selectDouble(parentProperty(), "width");
 DoubleBinding parentHeight = Bindings.selectDouble(parentProperty(), "height");
    //the radius will be the min of the two
 NumberBinding radius = Bindings.min(parentWidth,parentHeight).divide(2);

Now if I put a lot of these in a gridPane I get a board, all of the same size! enter image description here

As far as I understand each cell of the gridpane resizes automatically to its content. The problem is, if I maximize the window containing this board and then resize back, I get the following board: enter image description here

Some rows are bigger than others :( What I think happened is that when maximized, the gridpane increased the size of each cell (stackpane). Since the size is bound to the ellipse radius, the ellipse radius increased accordingly. But when I then make the window smaller, the gridpane doesn't resize filled cells probably because it's trying fit the now big ellipses.

How do I avoid this artifact? I know I can add column constraints objects, but do I have to add one FOR EACH COLUMN AND EACH ROW? That's a lot of objects. Is there any other way?

1
1
10/20/2013 7:41:27 PM

Accepted Answer

Turns out the answer was rather simple. The gridpane didn't resize the cell becuse the cell is a StackPane and stackpane minHeight/minWidth is equal to its component so it would refuse to resize. Using setMinSize(0,0) works just fine

2
10/20/2013 10:46:23 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon