英文:
ListView auto-scroll only working when manually scrolled to the bottom once
问题
我对JavaFX ListView组件进行了扩展:
HL7ListView extends ListView<String>
目标是在添加新项时,使其自动滚动到父级ScrollPane。我有一个构造函数,它接受要操作的ScrollPane作为参数,并创建了一个addItem()方法如下:
public void addItem(String item)
{
List<String> items = getItems();
items.add(item);
scrollTo(items.size());
scrollPane.setVvalue(1.0);
}
自动滚动仅在我手动滚动到底部一次以"启动它"时才有效,如果这有意义的话。一旦滚动到底部,新项目就会如预期般工作,滚动条会自动下滑,跟随ListView的内容。不确定问题可能出在哪里,显然,滚动条只在需要时才会出现,不确定这是否与此有关。
有什么想法吗?
英文:
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?
答案1
得分: 1
我认为您不需要用父级ScrollPane
包装您的ListView
,因为ListView已经内置了滚动条。您可以使用scrollTo()直接滚动到最后一个索引。
ListView<String> listView = new ListView<>();
listView.getItems().addAll(items);
listView.scrollTo(listView.getItems().size() - 1);
编辑
在您的扩展(HL7ListView
)中,使用super.scrollTo
而不是this.scrollTo
似乎也可以工作,
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);
}
}
附注:我尝试按照您的方法包装在
ScrollPane
中,查看了这个 QA - 似乎不起作用,看起来有点问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论