parsecer
parsecer

Reputation: 5176

FlowPane doesn't take up 100% of its outer HBox width

This is how it looks running:

enter image description here

This is how it looks when resized to full screen:

enter image description here

As can be seen the blue of the FlowPane doesn't span the whole red of the HBox.

How do I get the FlowPane span 100% of the HBox width, while keeping both the HBox centered inside the grid and the FlowPane centered inside HBox and FlowPane & HBox auto-resizing when a window gets resized? So that letters C and D at least fit on the first raw in the smaller window and all letters fit on the bigger window.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class TestFlowPane extends Application {
    private GridPane gridPane;
    private Scene scene;

    @Override
    public void start(Stage applicationStage) {
        gridPane = new GridPane();
        scene = new Scene(gridPane, 500, 400);
        ColumnConstraints column = new ColumnConstraints();
        column = new ColumnConstraints();
        column.setPercentWidth(100);
        gridPane.setAlignment(Pos.CENTER);
        gridPane.getColumnConstraints().add(column);

        gridPane.setPrefSize(500, 400);
        gridPane.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);

        gridPane.setPadding(new Insets(5, 20, 20, 20));
        gridPane.setHgap(10);
        gridPane.setVgap(10);

        HBox panelBox = new HBox();
        panelBox.setStyle( "-fx-background-color:   red; ");
        panelBox.setSpacing(20);
        panelBox.setAlignment(Pos.CENTER);
        FlowPane flowPane = new FlowPane();
        flowPane.setStyle( "-fx-background-color:   blue; ");

        String[] names = {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "BBBBBBBBBBBBB", "C", "D", "E"};
        for (String n: names) {
            Label label = new Label(n);
            label.setStyle( "-fx-text-fill:   white; ");
            flowPane.getChildren().add(label);
        }
        flowPane.setHgap(5);
        flowPane.setVgap(10);
        panelBox.getChildren().add(flowPane);
        gridPane.add(panelBox, 0, 0);

        applicationStage.setScene(scene);
        applicationStage.show();
    }
}

Upvotes: 0

Views: 317

Answers (1)

aziz k'h
aziz k'h

Reputation: 775

you need to add :

HBox.setHgrow(flowPane, Priority.ALWAYS);

after

panelBox.getChildren().add(flowPane);

and this will work

Upvotes: 2

Related Questions