1

I was wondering if anyone knew of a Swing-based alternative to JTable that handled its own scrolling instead of having to be put in a JScrollPane.

Let me explain why. I am currently working on a project where I will need to display up to 1.6 GB of data from a database in a tabular format. I've spent the last week and a half researching JTable and all of its associated classes. What I've learned leads me to believe that JTable is inadequate for reasons which follow.

JTables with datasets too large to be displayed on screen need to be put into a JScrollPane which acts as a moving window over components that are too large to fit on screen. This satisfies the requirement of being able to have an absurdly large JTable that doesn't use paging. However, the entirety of the JTable is still created which results in the TableModel being asked for all of the data. I may be wrong on that point, but my practical experience and a lack of documentation about the underpinnings of this stuff suggests that I'm not.

I need a JTable that will continuously scroll over an enormous dataset that is paged from the database and cached (only about 100-300 records) in the TableModel side that (PAY ATTENTION! THIS IS THE IMPORTANT PART) only asks for the data in the visible area of the table. My experience suggests that the standard JTable in a JScrollPane will ask for ALL of the data regardless of what is visible preventing any sort of back-end paging from working. Tell me I'm wrong and show me a working counterexample or point me to a third party component that behaves the way I need.

Stuporman
  • 773
  • 6
  • 26
  • 1
    Is it not really the table *model* which defines how memory efficient the table is? I.e. define a bespoke [TableModel](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html) that handles the data effectively and let the scroll pane just handle the visual stuff. – Duncan Jones Nov 01 '12 at 16:05
  • Also see http://stackoverflow.com/q/1380677/1076463 – Robin Nov 01 '12 at 16:20
  • @DuncanJones I'm not asking for a JTable that handles its own data. I'm asking for a JTable that handles its own scrolling because I am under the impression that the JTable doesn't care what part of itself is visible through the JScrollPane and just calls getValueAt() until it populates all of its rows. I'm probably wrong about that, but my experience so far shows that to be the case. – Stuporman Nov 01 '12 at 17:04
  • Couldn't you use a JTable / JScrollPane with a table model that contains two data areas? One would be the ResultSet of the database query. The other would contain 200 or so rows from the result set that are displayed in the JTable. Your table model would have to process the JScrollPane scroll actions so that the display would always contain the 200 or so rows centered around the rows currently displayed. – Gilbert Le Blanc Nov 01 '12 at 17:25
  • 1
    everything you want is pagination, managed on SQL side or on GUI side – mKorbel Nov 01 '12 at 17:26
  • @GilbertLeBlanc That's the basic idea behind what I'm looking for. I want to know if there is anything freely available that already behaves that way because I'm limited by time. – Stuporman Nov 01 '12 at 18:09
  • @mKorbel That's exactly what I'm saying. I'm looking for something pre-canned that does that so I don't have to invest another month trying to make that work. Specifically, pagination on the SQL side because the requirement is continuous scrolling across the whole dataset. – Stuporman Nov 01 '12 at 18:10
  • 2
    @Stuporman: maybe this will help: http://robbyvandamme.wordpress.com/2006/08/21/paging-in-a-swing-jtable/ – Gilbert Le Blanc Nov 01 '12 at 18:15
  • @GilbertLeBlanc You know, I tried doing something like that, but I must have done it wrong because it didn't work at all. Put that in a full-fledged answer, and I'll try it again and let you know. – Stuporman Nov 01 '12 at 18:34
  • this is one of [Great Paginations in JTable](http://java-swing-tips.blogspot.sk/search?q=pagination) by @aterai, – mKorbel Nov 01 '12 at 19:05
  • Why is everyone so hesitant to submit their answers as actual answers? – Stuporman Nov 01 '12 at 19:15

1 Answers1

4

There's no general purpose solution for this, as the implementation depends on how you intend to partition your data model into manageable pieces. As suggested here, you can partition an arbitrary List<Record> using the subList() method. You can expose individual models comprising each partition as shown here. See also this alternative approach using SQL.

I need continuous scrolling and not paging.

Your implementation of TableModel can use a SwingWorker that continually updates the view, as shown in this example or the API.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • It seems like you are recommending traditional paging. Maybe I didn't make it clear in my question, but I need continuous scrolling and not paging. – Stuporman Nov 01 '12 at 19:12
  • Yes, I was reading comments and overlooked your update. I've elaborated above. See also this [example](http://stackoverflow.com/a/7519403/230513) that addresses scrolling in the face of continual updates. – trashgod Nov 01 '12 at 19:41
  • I think when I'm done implementing one of these solutions, I'll start an open source Swing project to eliminate most of the boilerplate involved in this process because it is a huge waste of time. – Stuporman Nov 02 '12 at 17:53
  • Here's an [example](http://stackoverflow.com/a/7776211/230513) of how `JTable` does flyweight rendering. – trashgod Nov 02 '12 at 18:13
  • What is the purpose of the flyweight rendering example? – Stuporman Nov 20 '12 at 19:46
  • In case you want to consider doing your own implementation. – trashgod Nov 20 '12 at 19:49