I made an extension of the JavaFX ListView component :
HL7ListView extends ListView<String>
The goal is for it to auto-scroll the parent ScrollPane as new items are added to it. I have a constructor that takes the ScrollPane in question as an argument and I made an addItem() method like this :
public void addItem(String item)
{
List<String> items = getItems();
items.add(item);
scrollTo(items.size());
scrollPane.setVvalue(1.0);
}
The auto scrolling only works when i manually scroll to the bottom once to "get it going" if that makes any sense. Once it is scrolled to the bottom, new items behave as expected and the scrollbar goes down automatically, trailing the ListView's content. Not sure what could be the problem here, obviously the scroll bar only appears when there is a need for it, not sure if that could have anything to do with this.
Any idea?
I think you DON'T need to wrap your ListView
with parent ScrollPane
as ListView has the built-in scroller. You can use scrollTo() to scroll with the last index directly.
ListView<String> listView = new ListView<>();
listView.getItems().addAll(items);
listView.scrollTo(listView.getItems().size() - 1);
Edit
Using super.scrollTo
instead of this.scrollTo
seems to work on your extenstion (HL7ListView
) as well,
class HL7ListView extends ListView<String> {
public HL7ListView() {
}
public HL7ListView(List<String> items) {
this.getItems().addAll(items);
super.scrollTo(getItems().size() - 1);
}
public void addItem(String item) {
this.getItems().add(item);
super.scrollTo(getItems().size() - 1);
}
}
PS: I tried to wrap with ScrollPane as your approach and went through this QA - and that's not working, it seems kinda bug.