buttonjavafxmenuhbox

Java FX out of the window screen


I wanna it will be okay when the number variables is changed, but when the are increased the button goes out from the window. How to fix it? Also how to put the bar down to the level of "10$", so they will be in the same row?

Before : Before

After : After

Here is my code :

VBox vboxBottom = new VBox();
HBox hboxBottomElements = new HBox(15);
HBox hboxBottomMain = new HBox(0);

Region region = new Region();
region.setPrefWidth(500);

hboxBottomElements.getChildren().addAll(visaLabel, separator2, adLabel, separator3, governRelationStatus, separator4, region, next);
hboxBottomElements.setPadding(new Insets(5));

vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));

vboxBottom.setPadding(new Insets(3));

hboxBottomMain.getChildren().addAll(new Group(moneyBox), vboxBottom);
hboxBottomMain.setPadding(new Insets(3));
layout.setBottom(hboxBottomMain);

Solution

  • By using a Group here

    vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));
    

    you're creating a layout structure that resizes hboxBottomElements to it's prefered size independent of the space available.
    HBox simply moves elements out the right side of it's bounds, if the space available does not suffice. This means if the Group containing moneyBox grows, the Button is moved out of the HBox...

    The following simpler example demonstrates the behavior:

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button("Do something");
        HBox.setHgrow(btn, Priority.NEVER);
        btn.setMinWidth(Region.USE_PREF_SIZE);
        Region filler = new Region();
        filler.setPrefWidth(100);
        HBox.setHgrow(filler, Priority.ALWAYS);
        Rectangle rect = new Rectangle(200, 50);
        HBox hBox = new HBox(rect, filler, btn);
    
        Scene scene = new Scene(hBox);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    This will resize filler to make the HBox fit the window.

    Now replace

    Scene scene = new Scene(hBox);
    

    with

    Scene scene = new Scene(new Group(hBox));
    

    and the Button will be moved out of the window...