views:

546

answers:

1

I've found a sample for a sorted JList, but my application is powered by an embedded H2 database so I'm wondering if there isn't a better way to implement this with that in mind. Especially considering the table in question could become enormously large, and duplicating all that data in a JList's list model seems to kinda defeat the point of having a database to manage it.

Is there a good way to do this? Or am I forced to cobble together some clumsy hack to allow the JList to "scroll" through dynamically queried chunks of data or something?

+2  A: 

Your JList's ListModel is responsible for exposing the backing data as an ordered list. ListModel's methods will be called by JList from the UI (AWT event) thread, so its performance needs to be pretty good. This is why most implementations have the ListModel's backing data in memory. I suppose you could implement ListModel with your database as the backing data. You'll most likely extend AbstractListModel to get the listener registration, and implement getElementAt(int) and getSize(). getElementAt would then be responsible for getting the object for a particular index. Keep in mind that JList will call getElementAt many times for different indices, so you may find yourself caching the results. Depending on how much data you're caching, you might just retrieve the entire dataset from the database.

Steve Kuo