@Override
protected void layoutChildren() {
super.layoutChildren();
Bounds rect = getLayoutBounds();
Node [] children = getChildren ().toArray(new Node[0]);
int count = children.length;
if (count == 0) return;
double width = rect.getWidth() - marginWidth.get() * 2;
double height = rect.getHeight() - marginHeight.get() * 2;
if (horizontal.get()) {
width -= (count - 1) * spacing.get();
double x = rect.getMinX() + marginWidth.get(), extra = width % count;
double y = rect.getMinY() + marginHeight.get(), cellWidth = width / count;
for (int i=0; i<count; i++) {
Node child = children [i];
double childWidth = cellWidth;
if (i == 0) {
childWidth += extra / 2;
} else {
if (i == count - 1) childWidth += (extra + 1) / 2;
}
child.resizeRelocate(x, y, childWidth, height);
x += childWidth + spacing.get();
}
} else {
height -= (count - 1) * spacing.get();
double x = rect.getMinX() + marginWidth.get(), cellHeight = height / count;
double y = rect.getMinY() + marginHeight.get(), extra = height % count;
for (int i=0; i<count; i++) {
Node child = children [i];
double childHeight = cellHeight;
if (i == 0) {
childHeight += extra / 2;