Juan
Juan

Reputation: 2099

What's the best way to add a bunch of Button to a ScrollPane?

I'm kinda new to JavaFx, for my application I need to set an indeterminate bunch of buttons on a part of the screen. Since I don't know how many buttons I'll need until the program is started, I thought on set a ScrollPane to this part of the screen, and there add dynamically a bunch of HBox containing the buttons (I use a List<> of buttons and a List<> of HBox, so I can create a new HBox for each 8 buttons).

The idea is use the ScrollPane to scroll between the different HBox which contains the buttons, so I don't need to always show all buttons. The problem is that it seems that you can't add directly a bunch of HBox to a ScrollPane. Is there anyway to perform this?? My code will be something like this:

public void startApp(int nDetect){   
    this.primaryStage = new Stage();
    this.nDetect = nDetect;
    BorderPane bp = new BorderPane();

    Group root = new Group();
    .
    .
    .
    LinkedList<Button> buttons = new LinkedList<>();
    LinkedList<HBox> boxes = new LinkedList<>();

    for(int i=0; i<this.nDetect; i++) {
        if(i%8 == 0){
            boxes.add(new HBox());
            boxes.get(i/8).setSpacing(5);
        }
        boxes.get(i/8).getChildren().add(buttons.get(i)) //add the button to the appropriate HBox
    }

    ScrollPane spane = new ScrollPane();

    for( HBox h : boxes){
        //add every element in "boxes" to the ScrollPane
    }
    bp.setTop(spane);
    root.getChildren().add(bp);
}

Upvotes: 2

Views: 5130

Answers (2)

jewelsea
jewelsea

Reputation: 159291

Sounds like you want your buttons laid out within a grid within a scroll pane.

The appropriate layout for that would be a GridPane.

button grid

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class ButtonLinesInScrollPane extends Application {

    private static final double BUTTONS_PER_LINE = 8;
    private static final double NUM_BUTTON_LINES = 8;
    private static final double BUTTON_PADDING   = 5;

    @Override
    public void start(Stage stage) {
        GridPane grid = new GridPane();
        grid.setPadding(new Insets(BUTTON_PADDING));
        grid.setHgap(BUTTON_PADDING);
        grid.setVgap(BUTTON_PADDING);

        for (int r = 0; r < NUM_BUTTON_LINES; r++) {
            for (int c = 0; c < BUTTONS_PER_LINE; c++) {
                Button button = new Button(r + ":" + c);
                grid.add(button, c, r);
            }
        }

        ScrollPane scrollPane = new ScrollPane(grid);

        stage.setScene(new Scene(scrollPane));
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}

Upvotes: 4

RDM
RDM

Reputation: 5066

A ScrollPane wraps another Node. Just make a single HBox (or VBox), set that as content of the scroll pane and add all Buttons to the box. The box will scale automatically and the scroll pane will stay the same size but automatically draw horizontal and/or vertical sliders as the content grows beyond its bounds.

Upvotes: 2

Related Questions