通过按钮滚动TableView [英] Scroll TableView via a Button
问题描述
背景
使用JavaFX,我渲染 TableView
和 按钮
并排。
Using JavaFX, I render a TableView
and a Button
side by side.
该表包含许多条目,这些条目多于表格大小。因此,TableView会自动显示向下滚动的滚动条。
The Table contains a number of entries which are more then the Table size. Hence TableView automatically shows a scrollbar for scrolling down.
问题
I想要使用按钮向下滚动表格。按下按钮,向下滚动表格5个条目。我找到了 scrollTo()
方法,这对于任务来说已经足够了,但是我需要一个索引来滚动。我没有,据我所知,没有getfirstvisibleindex方法(或类似的东西,你明白了)。
I want to scroll down the table by using the button. So pressing the button, scroll down the table by 5 entries. I found the scrollTo()
method, which would be sufficient for the task, but I need an index to scroll to. Which I do not have, as far as I understand there is no "getfirstvisibleindex" method (or something similar, you get the idea).
如何确定表格要滚动到的行索引?
How can a determine the table row index to scroll to?
编辑
Edit
谢谢大家帮助我。为了完整起见,我还发布了我的最终解决方案,该解决方案基于/复制在下面提供的链接上, Link1 和 Link2 。以下方法是作为TableView的扩展版本的一部分实现的,并且将由按钮调用。 (边线节点:我不确定是否真的有必要检查结尾是否有必要。)
Thanks everyone for helping me out. For the sake of completeness I post "my" final solution as well, which is based/copied on the link provided below, Link1 and Link2. The following method is implemented as part of an extented version of TableView and will be called by a button. (Side Node: I am not sure if the check for the end is reached cased is really necessary.)
public void scrollbartest(){
TableViewSkin skin = (TableViewSkin) getSkin();
VirtualFlow vf = null;
ObservableList<Node> kids = skin.getChildren();
if (kids != null && !kids.isEmpty())
{
vf = (VirtualFlow)kids.get(1);
}
if (vf == null) { return; }
if (vf.getFirstVisibleCell() == null) { return; }
int first = vf.getFirstVisibleCell().getIndex();
int last = vf.getLastVisibleCell().getIndex();
int numberofrows = this.getItems().size();
if(last != numberofrows-1){
this.scrollTo(first+1);
}
}
推荐答案
我以tableview教程为例: http://docs.oracle .com / javafx / 2 / ui_controls / table-view.htm
I'm using the tableview tutorial as an example: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm
TableView由您定义的某种对象类型的可观察列表支持。在本教程中,他们使用:
The TableView is backed by an observable list of some object type you define. In the tutorial, they use:
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("Jacob", "Smith", "jacob.smith@example.com"),
new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
new Person("Ethan", "Williams", "ethan.williams@example.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com")
);
除以下行:
table.setItems(data);
数据列表对象有一个大小的方法,对你的情况很有用。在按钮操作中,您可以查找列表的大小并向前设置scrollTo()索引5个位置。只需跟踪按钮操作定义之外的起始索引。
The data list object has a size method which would be useful for your case. In your button action, you could look up the size of the list and set the scrollTo() index 5 positions forward. Just keep track of what the starting index is outside of the button action definition.
这篇关于通过按钮滚动TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!