I would like to do in JavaFX getting of the TableView from the database, but I have a lot of records it needs to load in small portions (eg. 50 position), and that it gets the remaining little by little as I scroll the scroll wheel. Any tips? or where can I find some good example?
Asked
Active
Viewed 264 times
0
-
You could try to check if the scrollbar is at the bottom, if it's true then you just have to add more elements to the `TableView` and repeat the process until you have no elements left. – aleb2000 Sep 06 '16 at 08:32
-
I have found [this example](http://www.superglobals.net/javafx-listview-lazy-loading/). It uses a `ListView` but the process is the same. – aleb2000 Sep 06 '16 at 08:34
-
ok, thanks. You can add as a response – Sep 06 '16 at 08:39
-
If you give me some time I can write an example using the TableView – aleb2000 Sep 06 '16 at 08:43
-
ok, i will be wait – Sep 06 '16 at 09:01
1 Answers
2
As an example I'll use a Person class that represent an element in the table.
public class Person {
private StringProperty firstName = new SimpleStringProperty(this, "firstName");
private StringProperty lastName = new SimpleStringProperty(this, "lastName");
public Person(String firstName, String lastName) {
setFirsName(firstName);
setLastName(lastName);
}
public void setFirsName(String firstName) {
this.firstName.set(firstName);
}
public String getFirstName() {
return firstName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public String getLastName() {
return lastName.get();
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
}
Here the Main class:
public class TableViewOnScroll extends Application {
private ObservableList<Person> peopleToAdd;
private int start = 0;
private int step = 50;
@Override
public void start(Stage primaryStage) throws Exception {
TableView<Person> tableView = new TableView<Person>();
ObservableList<Person> displayedPeople = getInitialPeople();
peopleToAdd = getPeopleToAdd();
TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
TableColumn<Person,String> lastNameCol = new TableColumn<Person,String>("Last Name");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
tableView.getColumns().setAll(firstNameCol, lastNameCol);
tableView.setItems(displayedPeople);
primaryStage.setScene(new Scene(tableView, 600, 400));
primaryStage.show();
// Be sure to put the listener after the stage is shown or the application will throw a NullPointerException
ScrollBar tableViewScrollBar = getTableViewScrollBar(tableView);
tableViewScrollBar.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double position = newValue.doubleValue();
ScrollBar scrollbar = getTableViewScrollBar(tableView);
if(position == scrollbar.getMax()) {
if(step <= peopleToAdd.size()) {
displayedPeople.addAll(peopleToAdd.subList(start, step));
start = step;
step += 50;
tableView.scrollTo(start);
}
}
}
});
}
public ObservableList<Person> getPeopleToAdd() {
ObservableList<Person> people = FXCollections.observableArrayList();
for(int i = 0; i < 500; i++) {
people.add(new Person("Patty", "Johnson"));
}
return people;
}
public ObservableList<Person> getInitialPeople() {
ObservableList<Person> people = FXCollections.observableArrayList();
for(int i = 0; i < 50; i++) {
people.add(new Person("John", "Smith"));
}
return people;
}
private ScrollBar getTableViewScrollBar(TableView<?> listView) {
ScrollBar scrollbar = null;
for (Node node : listView.lookupAll(".scroll-bar")) {
if (node instanceof ScrollBar) {
ScrollBar bar = (ScrollBar) node;
if (bar.getOrientation().equals(Orientation.VERTICAL)) {
scrollbar = bar;
}
}
}
return scrollbar;
}
public static void main(String[] args) {
launch(args);
}
}
The use of the getInitialPeople() and getPeopleToAdd() is only to populate the lists with some random data.

aleb2000
- 452
- 4
- 10
-
Ok, if you have problems or questions about the code I posted, I will be glad to help you. – aleb2000 Sep 06 '16 at 09:49