I have a ScrollPane for displaying content. When I set the content of the ScrollPane, I will bind its prefWidthProperty so that its prefWidth will be changed when the ScrollPane’s prefWidth changed.
public void setContent(VBox content) { // I want all the elements in this ScrollPane to be arranged vertically, so I'm using VBox here
content.prefWidthProperty().bind(CONTENT.prefWidthProperty().subtract(CONTENT.getPadding().getRight()).subtract(CONTENT.getPadding().getLeft()));
CONTENT.setContent(content); //The 'CONTENT' is my ScrollPane
}
I have a HBox(manageBox), which has a Label and a Button. I want the HBox’s prefWidth equals the ScrollPane content’s prefWidth and the two elements in the HBox will be in the two side. One is in the left and another is in the right. So I bind the spacing with the HBox’s prefWidth and the two elements’ prefWidth.
final VBox HOME_CONTENT = new VBox();
HBox manageBox = new HBox();
manageBox.setStyle("-fx-border-width: 1; -fx-border-color: black;");
Label list = new Label("Element List");
Button add = new Button("Add New");
manageBox.spacingProperty().bind(manageBox.prefWidthProperty().subtract(list.prefWidthProperty()).subtract(add.prefWidthProperty()));
manageBox.prefWidthProperty().bind(HOME_CONTENT.prefWidthProperty());
manageBox.getChildren().addAll(list, addRMSC);
HOME_CONTENT.getChildren().add(manageBox);
setContent(HOME_CONTENT);
Now, when I setup my application, it will display like this. application image
It looks like the HBox’s prefWidth bigger than the ScrollPane content’s prefWidth. But when I use
setContent(new VBox(manageBox))
, It will be equal again, but the label and the button will stick highly together like spcaing is zero! It’s so strange!
I have no idea why it is. Please help me out.
I searched the api docs of JavaFX but still have no idea.