Event for JTable + TableModel cache for lazy instantiation?

Scenario:

you are using a JTable with a custom TableModel to view the contents of some collection located in the database or on the web or whatever.

The crude way to make this work is to download the entire collection at once. Let's say it's impractical due to the required resources.

An easy way to solve this problem is to select rows on demand, one row at a time, since the JTable displays each row and calls TableModel.getValueAt (); if necessary. This causes a lot of database calls.

Is there a way to listen for scroll / view events for the JTable to figure out which rows it will display until it displays each cell? If so, I would like to intercept and force my custom TableModel to prefetch one page at a time.

edit: to clarify, here is to account for the contents of a group of visible table rows in one batch, instead of getting the contents of each row yourself.

+2


source to share


2 answers


Basically, this is exactly what the JTable allows. as far as I know, if the getRowCount () method accurately reflects the number of records, then when the cells are colored, only the visible part is requested. I don't think prefetching when listening in a viewport will be faster.

you can wait for all getvalue requests. write them down, return "null" or an already cached value. then after getvalue is not called, say 20ms makes the actual request for all the cells written. and fire rowUpdated events on the model, so the JTable is repainted again.



** [edit] ** You can keep a list of the latest entries requested on the model. your list should not be more than the number of lines visible on the screen. after getValue () is not requested for a few ms, you can do this async bulk request for external

The only catch here is the sort / filter algorithm. When you ask for a viewport and let the data depend on it, then there is 1-1 between your data and the view. Something JTable doesn't have. But I think there is no way around this. I would include an IDE debugger for digging in sun code. and then see how their render implementation detects which lines are being redrawn. I donโ€™t know by heart.

+2


source


Take a look at http://www.javaworld.com/javaworld/javatips/jw-javatip137.html . This article has a custom tableModel that can fetch "chunks" of rows from the database



Another script solution, although not exactly what you are looking for, would be lazy loading each row instead of prefetching. See my own post (google search for "JTable is lazy loaded to database") on how to do this. The idea is that when a row is given to the tablemodel that is not cached / loaded, it will return the "wait..retrieving" string for each column (assuming all columns are rows). and at the same time it will schedule the task on a different thread (using ExecutorService). The task will then fetch data from the database and update the data. In my post, I actually used Beans bindings, so a custom list was used instead of a custom y model table. But I'm sure you can extrapolate this idea.

+3


source







All Articles