I'm trying to make a honeycomb flow with buttons in JavaFX with so far a FlowPane
So far I got it half working by using a negative VGap on my FlowPane, but as soon as I resize it and make the 3-4 row go to 3-3-1 it obviously goes wrong
I'm trying to find a solution that will keep the honeycomb layout with variable amounts of buttons, but so far I havent had much success. So I was wondering if anyone knows a solution for this.
edit: here is the basic code I have at the moment
FlowPane root = new FlowPane();
root.setHgap(3.0); root.setVgap(-23.0);
root.setAlignment(Pos.CENTER);
Button[] array = new Button[7];
for(int i = 0; i <= 6; i++){
Button button = new Button();
button.setShape(polygon); (made a polygon in the same of a hexagon)
array[i] = button;
}
root.getChildren().addAll(array);
Predetermined columns/ rows
This is a bit more convenient than using a
FlowPane
to place the fields.You can observe that using column spans you can place the
Button
s in aGridPane
: Every field fills 2 columns ofsqrt(3/4)
times the field height; the odd/even rows start at column 0/1 respectively. Every field fills 3 rows and the size of the column constraints alternate between one quarter and one half of the field height.Example
FlowPane
-like behaviorMaking the x position depend on the row the child is added is not a good idea in a
FlowPane
. Instead I recommend extendingPane
and overridinglayoutChildren
method place the children at custom positions.In your case the following class could be used: