0

I am facing an issue with ScrollPane, which is performs extremely slow on the relatively small amount of buttons as a content. I have 200+ ImageView inside and it works perfect, but after adding 200+ buttons for these images, scrolling became completely unusable. Even more, if I am trying to add just buttons, without images, they still make ScrollPane extremely slow:

public class TestApplication extends Application {

    @Override
    public void start(Stage stage) throws IOException {
        VBox container= new VBox();
        ScrollPane root= new ScrollPane(container);

        for (int i = 0; i < 400; i++) {
            container.getChildren().add(new Button("Hello"));
        }

        stage.setScene(new Scene(root, 500, 250));
        stage.show();
    }

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

With 100 buttons is works, with 200 buttons it works slowly, with 400 buttons it is hard to say it works at all.

Is it the problem with my code or this ScrollPane so slow working with buttons?

Zendar
  • 25
  • 6
  • 2
    This works fine for me. What JDK/JavaFX versions are you using? – James_D May 15 '23 at 19:54
  • 1
    My guess is that you should be using `ListView` or `TableView`. – SedJ601 May 15 '23 at 21:27
  • 2
    Also works fine for me on my computer (Windows 11, Intel i5 10th gen, 8gb ram, integrated graphics, Java 20.0.1, JavaFX 20.0.1). I don't start to notice any lag until about 1000 buttons. Though note that if I needed to display a list of this many buttons then I'd be using a `ListView`, not a `ScrollPane` + `VBox`. A `ListView` is "virtual". It may have millions of items, but only what's currently being displayed is actually rendered (about 10-100 cells, depending on the size of the list view). – Slaw May 15 '23 at 21:45
  • This [example](https://stackoverflow.com/a/44823611/230513) illustrates how it scales; use a custom cell factory for the buttons, for [example](https://stackoverflow.com/q/15661500/230513). – trashgod May 15 '23 at 22:11

0 Answers0