Let the layout pane do the layout for you.
If the layout pane you are using isn't giving you the exact layout you want then either:
- Adjust constraints on the layout pane and its child nodes.
- Compose multiple different types of layout panes.
- Use a different built-in layout pane type.
- Create your own layout pane type.
For your method you have a parameter indicating whether to size all of the child nodes the same size. A pane which sizes all child nodes the same size is a TilePane, so you could choose that to layout your elements. A GridPane will also work because it has configurable constraints to size elements the same size. A stock HBox won't work directly because it doesn't have a property to size all child elements the same size. You could subclass HBox to do this if you wished (by overriding layoutChildren()).
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SameSizeButtons extends Application {
@Override
public void start(Stage stage) {
VBox layout = new VBox(
10,
createHorizontalButtonBox(
Arrays.stream("All buttons the same width".split(" "))
.map(Button::new)
.collect(Collectors.toList()),
Pos.CENTER,
10,
true
),
createHorizontalButtonBox(
Arrays.stream("All buttons different widths".split(" "))
.map(Button::new)
.collect(Collectors.toList()),
Pos.CENTER_RIGHT,
10,
false
)
);
layout.setPadding(new Insets(10));
layout.getChildren().forEach(node ->
node.setStyle("-fx-border-color: red; -fx-border-width: 1px;")
);
stage.setScene(new Scene(layout));
stage.show();
}
public static Pane createHorizontalButtonBox(
final List<Button> buttons,
final Pos alignment,
final double spacing,
final boolean sameWidth) {
return sameWidth
? createSameWidthHorizontalButtonBox(
buttons,
alignment,
spacing
)
: createDifferentWidthHorizontalButtonBox(
buttons,
alignment,
spacing
);
}
private static Pane createSameWidthHorizontalButtonBox(
List<Button> buttons,
Pos alignment,
double spacing)
{
TilePane tiles = new TilePane(
Orientation.HORIZONTAL,
spacing,
0,
buttons.toArray(
new Button[buttons.size()]
)
);
tiles.setMinWidth(TilePane.USE_PREF_SIZE);
tiles.setPrefRows(1);
tiles.setAlignment(alignment);
buttons.forEach(b -> {
b.setMinWidth(Button.USE_PREF_SIZE);
b.setMaxWidth(Double.MAX_VALUE);
});
return tiles;
}
private static Pane createDifferentWidthHorizontalButtonBox(
List<Button> buttons,
Pos alignment,
double spacing)
{
HBox hBox = new HBox(
spacing,
buttons.toArray(
new Button[buttons.size()]
)
);
hBox.setAlignment(alignment);
buttons.forEach(b ->
b.setMinWidth(Button.USE_PREF_SIZE)
);
return hBox;
}
public static void main(String[] args) {
launch(args);
}
}