JavaFX 2 buttons size fill width and are each same width?

后端 未结 2 1277
伪装坚强ぢ
伪装坚强ぢ 2021-01-04 12:32

Working in Java FX 2.2. The Scene has a horizontal width that is fixed but is unknown at compile time. I want to place 2 or more buttons in a horizontal row that completel

2条回答
  •  甜味超标
    2021-01-04 13:05

    A bit cleaner solution with property binding:

        HBox buttonLayout = new HBox();
        buttonLayout.getChildren().add(button1);
        buttonLayout.getChildren().add(button2);
    
        int btnCount = buttonLayout.getChildren().size();
        button1.prefWidthProperty().bind(buttonLayout.widthProperty().divide(btnCount));
        button2.prefWidthProperty().bind(buttonLayout.widthProperty().divide(btnCount));
    

提交回复
热议问题