JavaFX - Is it possible to have a scroll bar in VBox?
Solution 1
Try putting VBox inside a scrollPane. With this approach, you don't have to worry about setting the prefHeight and prefWidth attributes. You can use 'USE_COMPUTED_SIZE' for the VBox.
Example: VBox is added inside a ScrollPane, and various elements like HBox, Labels, and rectangles are added dynamically.
Solution 2
You can use ScrollPane
.
Example:
ScrollPane scroll = new ScrollPane();
scroll.setContent(checkboxContainer);
Solution 3
you can add a scroll bar with VBox
Group root = new Group();
ScrollBar sc = new ScrollBar();
sc.setMin(0);
sc.setOrientation(Orientation.VERTICAL);
//set other properties
VBox vb = new VBox();
//add childrens to Vbox and properties
root.getChildren().addAll(vb, sc);
sc.valueProperty().addListener(new ChangeListener<Number>() {
public void changed(ObservableValue<? extends Number> ov,
Number old_val, Number new_val) {
vb.setLayoutY(-new_val.doubleValue());
}
});
further you can refer here
If you want to change layout of check boxes try different layout pane(GridPane,TilePane ,etc) instead of VBox.
Related videos on Youtube
Novi
I worked as Web Developer at vendershop.com. i just started recently last november 2016
Updated on October 05, 2022Comments
-
Novi over 1 year
i'am working a project that involves array of checkboxes. but i encounter a problem when i'm adding all the checkboxes in VBox. here is my screenshot below
Other checkboxes cannot be viewed.
here is my code for checkboxes
public void initializeSenatorLists() { CheckBox []chckSenators = new CheckBox[senators.length]; for(int s=0; s < senators.length; s++) { chckSenators[s] = new CheckBox(senators[s]); chckSenators[s].setStyle("-fx-font-size:15px;"); chckSenators[s].setTextFill(Color.WHITE); senVbox.getChildren().add(chckSenators[s]); } for(CheckBox cbSen:chckSenators) { cbSen.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { if(cbSen.isSelected()) { senatorLimitVote++; votedSenators.add(cbSen.getText()); }else { votedSenators.remove(cbSen.getText()); senatorLimitVote--; } } }); } }
What i want to do to my checkboxes is this
I hope you can help me.
-
Abhi almost 6 yearsGlad to know that it helped you JustIce
-
Hasen about 2 yearsIt causes huge issues though. All the elements are vertically aligned when within the VBox but once but into a scrollpane all the vertical alignment is lost, and there seems to be no way to set alignment for items within scrollpane.