Tony
Tony

Reputation: 507

setFillWidth doesn't work in JavaFX

I have this code:

public class Minesweeper extends Application {

private MenuBar menuBar;
private Stage primaryStage;
private final VBox vbox = new VBox(8);
private GridPane gridpane;

@Override
public void start(Stage primaryStage) {

    this.primaryStage=primaryStage;
    /*createMenu();

    //ImageViewPane viewPane = new ImageViewPane(gridpane);

    vbox.getChildren().addAll(menuBar,gridpane);
    vbox.setBackground(new Background(fills));*/

    HBox hbox = new HBox(50);
    hbox.setAlignment(Pos.CENTER); // default TOP_LEFT

    VBox vbox1 = new VBox();
    //vbox1.setAlignment(Pos.BOTTOM_CENTER);
    vbox1.setStyle("-fx-border-style: solid;"
            + "-fx-border-width: 1;"
            + "-fx-border-color: black");
    vbox1.setPrefSize(300, 300);
    vbox1.setFillWidth(true);

    VBox vbox2 = new VBox(10);
    vbox2.setAlignment(Pos.CENTER);
    vbox2.setStyle("-fx-border-style: solid;"
            + "-fx-border-width: 1;"
            + "-fx-border-color: black");

    VBox vbox3 = new VBox(20);
    vbox3.setAlignment(Pos.TOP_CENTER);
    vbox3.setStyle("-fx-border-style: solid;"
            + "-fx-border-width: 1;"
            + "-fx-border-color: black");

    for (int i = 0; i < 5; i++)
    {
        Button bt = new Button("Button " + (i+1));
        Button bt2 = new Button("Button " + (i+1)); // unfortunately there´s no "clone" or "copy" method
        Button bt3 = new Button("Button " + (i+1));

        vbox1.getChildren().add(bt);
        vbox2.getChildren().add(bt2);
        vbox3.getChildren().add(bt3);
    }

    hbox.getChildren().addAll(vbox1, vbox2, vbox3);
    Scene scene = new Scene(hbox, 350, 250); // the hbox is the root node
    //Scene scene = new Scene(vbox,600,600);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Minesweeper");
    primaryStage.show();

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

and i want the buttons in vbox1 to fill the remaining space (setfillwidth) but it doesn't work. Maybe iam not using it correctly.

Can someone explain this to me.

Thanks

Upvotes: 0

Views: 1331

Answers (1)

Dmitry Zinkevich
Dmitry Zinkevich

Reputation: 3623

Try setting maxWidth property of the button to infinity to allow it taking all the available space.

bt.setMaxWidth(Double.POSITIVE_INFINITY);

See this answer for details.

Upvotes: 3

Related Questions