I have a TableView with more than 5000 entries. Is there a way to load from database only the first 20 entries (TableView can display a max of 20 entries at a time) and when the user scrolls down, the next 20 entries will be loaded from database. Or do you guys have a better way of keeping the RAM usage to a minimum?
-
1Just use the correct query. – SedJ601 Jul 01 '19 at 13:13
-
The other thing you can do is use ideas from https://stackoverflow.com/questions/15349185/javafx-tableview-paginator. – SedJ601 Jul 01 '19 at 13:18
-
You could also look around for Lazy-loading implementations which it sounds like your looking for – Matt Jul 01 '19 at 19:52
-
@Sedrick Can you give an example for a correct query? – Chiggiddi Jul 02 '19 at 09:11
-
1It can look something like `SELECT * FROM requests LIMIT 20` -> This would retrieve rows 1-20. `SELECT * FROM requests LIMIT 20, 20` -> This would retrieve rows 21 - 40. `SELECT * FROM requests LIMIT 40, 20` -> This would retrieve rows 41 - 60. These queries need `ORDER BY` some row. – SedJ601 Jul 02 '19 at 13:56
-
1I agree with @DaveB answer. TableView can handle 5000 entries easily in most cases. Are you experiencing a problem or just think you can do a better job than the native implementation? – SedJ601 Jul 02 '19 at 14:02
-
2I agree, I have `TableView` with over 100,000 rows and even searching within it is very fast. – trilogy Jul 02 '19 at 14:26
-
1@Sedrick I think the query is not the issue here. I guess I did overthink the issue. As DaveB has pointed out. 5000 entries should be alright. Thanks for your reply! Much appreciated. – Chiggiddi Jul 03 '19 at 19:46
1 Answers
TableView won't create 5000 rows of graphics if the viewport is only 20 rows high. It will probably only have about 25 rows, enough to preload a few rows off screen for performance. Screen table rows are recycled with new table model data as you scroll up and down. So the idea that you can somehow "improve the performance" is a bit out of wack to start with.
The main problem with lazy loading is that it isn't really compatible with a lot of the elements of a TableView. For instance, what should happen when the user clicks on a column header to sort? How big should the scroll cursor be? What happens if the user drags the scroll cursor down to the bottom?
5000 items doesn't seem that large to me. Your table model should only have the data required for row display, which in most cases boils down to a few string and number properties. So unless your table is hundreds of columns wide, your entire table should be a reasonable size. 200 bytes per row is only 1MB for the whole table model.

- 1,836
- 1
- 15
- 13
-
Yes, that is totally true. I just test it and JavaFX has no trouble displaying it without performance hiccups. Thanks a lot for your concise explanation Dave. It helped me a lot in understanding the TableView inner workings! – Chiggiddi Jul 03 '19 at 19:43