问题
Background
Using JavaFX, I render a TableView and a Button side by side.
The Table contains a number of entries which are more then the Table size. Hence TableView automatically shows a scrollbar for scrolling down.
The Problem
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
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);
}
}
回答1:
I'm using the tableview tutorial as an example: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm
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")
);
Along with the line:
table.setItems(data);
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.
来源:https://stackoverflow.com/questions/24193840/scroll-tableview-via-a-button