JavaFX - How to set same width for all buttons before displaying the Stage?


Question

I want to set the same width for the buttons of a pane, but before displaying the Stage there is no button width. How can I get the width without displaying the Stage?

Example of code that does not work because width is not defined:

public static HBox createHorizontalButtonBox(final List<Button> buttons, final Pos alignment, final double spacing, final boolean sameWidth) {
    HBox box = new HBox(spacing);
    box.setAlignment(alignment);
    box.getChildren().addAll(buttons);

    if (sameWidth && buttons.size() > 1) {
        double max = maxWidth(buttons);
        for (Button b : buttons) {
            b.setPrefWidth(max);
        }
    }

    return box;
}
1
0
4/18/2014 10:12:22 PM

Accepted Answer

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:

  1. Adjust constraints on the layout pane and its child nodes.
  2. Compose multiple different types of layout panes.
  3. Use a different built-in layout pane type.
  4. 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()).

sizing

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);
    }
}  
7
4/19/2014 6:27:32 AM

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