Novi
Novi

Reputation: 91

JavaFX - Is it possible to have a scroll bar in VBox?

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

https://i.sstatic.net/l0fw3.png

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 https://i.sstatic.net/ODcDb.png

I hope you can help me.

Upvotes: 9

Views: 22849

Answers (3)

Abhi
Abhi

Reputation: 159

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.

Upvotes: 9

The Well
The Well

Reputation: 894

You can use ScrollPane.

Example:

ScrollPane scroll = new ScrollPane();
scroll.setContent(checkboxContainer); 

Upvotes: 4

Sarfaraz Khan
Sarfaraz Khan

Reputation: 2186

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.

Upvotes: 1

Related Questions