2

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);
}
Juan
  • 1,949
  • 1
  • 15
  • 22

2 Answers2

4

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); }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
2

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.

RDM
  • 4,986
  • 4
  • 34
  • 43
  • The problem is that I want to organize buttons in lines of 8 (there'll always be at least a line of 8 visible buttons, and you have to scroll down to see the next line of 8). Adding every button to an HBox and this to the ScrollPane, it shows a single line with horizontal scroll – Juan Mar 23 '15 at 15:19
  • Then you should use a `GridPane`, as suggested by @jewelsea – RDM Mar 24 '15 at 09:40