I have a ScrollPane in my scene and would like to add a series of Nodes to it. The exact amount must be dynamic. I've been testing the concept with Label nodes and can successfully add as many Labels as I want to the content of the ScrollPane (currently a VBox).
My problem is figuring out how to allow the user to scroll normally through the ScrollPane full of Nodes. The content matches pace with the scrollbar; that is, when I scroll down, the content moves down. I scroll up, the content scrolls up. So if I have 10 Labels, only five of which are currently visible, the instinctive action to see the other five would be to scroll down. Unfortunately, this just moves the entire content down, exposing empty space up top and hiding more of the Labels.
I can post some sample code if that helps, but the gist of my plan of attack is this:
ScrollPane sp = new ScrollPane();
VBox content = new VBox();
sp.setContent(content);
for (int i = 0; i < 10; i++)
{
Label label = new Label("Label " + i);
content.setPrefHeight(content.getPrefHeight() + label.getPrefHeight());
content.getChildren().add(label);
}
Because I use a VBox, the Labels are stacked (in the Y axis) atop one another, which is what I want. As more Labels are added, the ScrollBar needs to reflect the increased size of the VBox.
I've tried a few approaches to get this to work, including ScrollPane.setVmax()
method, which is ok, but I still have the issue of the scroll direction moving the content in the wrong direction. I've also tried an EventHandler
and EventFilter
to modify values accordingly, still without solving the problem of scrolls not moving the content in the right direction.
The answer seems so simple, yet the it continues to evade me, even after many long hours messing around with the code.
EDIT:
jewelsea's suggestion of using a ListView
solved my problem. However, I remain curious as to why using a ScrollPane
did not fare so well. Additionally, I've run into odd behavior with the ListView
implementation: I use a button to manually add a new Label
to the view. Every now and then, the view will freeze up after adding a new Label
. It locks up for a few seconds before either updating the scroll position or displaying the newly added Label
. I don't know if this is because there are about 20 or so Label
instances being managed by the view or if something more insidious is at play.
A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
The ScrollPane allows the application to set the current, minimum, and maximum values for positioning the contents in the horizontal and vertical directions. These values are mapped proportionally onto the layoutBounds of the contained node.
The ListView class represents a scrollable list of items.
In regards to the scrollpane issues, I know what you're looking to do is possible because I'm doing something very similar. Based on the code snippet you gave above I would suggest trying few things...
Hope this helps you out!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With