JTable to render unknown number of rows

I have a database query returning a large number of rows. JDBC

doesn't tell you how many rows are returned and there are too many rows to count before I show it in JTable

.

So the model I'm working with is this Iterable<R>

, but unfortunately TableRowModel

needs to know the row count up.

I can think of several strategies that might work:

  • Load all the lines, but do it in the background and add them to the model in batches.
  • Somehow detect requests to render the bottom rows (through the model I guess) and load more rows dynamically if that happens.
  • You have a button on the screen to explicitly load more rows.
  • Some component non-JTable

    that supports rendering lines without knowing the number of lines beforehand (ideal, but I can't seem to find it.)

I am wondering if there is a "normal" way to do this, because it is not uncommon for me to see this in the UI. Several tools JDBC

I've tested (I figured this would be the best choice) seem to either eagerly download the results or publish them (as a user, I really don't like paging in graphical applications, so I'd like to avoid that.)

Operations such as Find on a table should ideally work in a "understandable" way, although ... so it doesn't seem to be too easy.

+3


source to share


2 answers


Use SwingWorker

in your implementation AbstractTableModel

. Have the work partition split the query so that the available number of records are available quickly and the rest in the background. Use PropertyChangeListener

to show progress and click the cancel button.



+3


source


You have 2 possibilities:

  • Write a query that returns the number of rows and then implements a table that tries to load rows from the database if the table wants to show rows that are not in the model. In this case, you need a third query that can index row loads (these queries are possible in MS-SQL and Oracle). For example: you load the first 100 rows and the number of results. If the table model is given rows from at positions 100 to 199, you create a new query that loads those row intervals (if possible for your database), or you try to iterate over your result set and load the next batch of data. But if the user presses the End key, which definitely needs the index to load (or you need to iterate over the full result set).
  • You are writing a model that shows the first batch (for example 100 lines). If the user is looking at the last line (model.getValueAt (line, col) -> col == 99), you just query the result set for the next batch and add new lines to the model.


The first option is better for the user (because the user can see how big the table is and can directly scroll to the last row), but the second is easier to implement because it doesn't need to index the row load.

+1


source







All Articles