1

I am trying to understand a Swing application specifically how tables work. There is a table that retrieves data from the database and displays the data on the JTable. I went through the code and worked out how the data is read from the database.

One thing that i seem to be struggling with is how the data is displayed on the table and how it is updated. I know how to display data on a table but the difference with this table is that the data is updated dynamically. i.e. Not all of the data that is retrieved from the database is displayed on the table.

It looks like the application displays a number of rows and when the user srolls the mouse or moves the scrollbar to the bottom/end of the table, the application automatically updates the table and displays more data (i can see that there is a slight delay when that happens).

For me to be able to work out how the above is impmlemented i need some more information on this technique i.e. what is this kind of technique called and what are the usual interfaces/classes that are used.

A link to an example of how the above is achieved would be very useful as then i need to see a simplified version first.

Any idea?

Thanks

wattostudios
  • 8,666
  • 13
  • 43
  • 57
ziggy
  • 15,677
  • 67
  • 194
  • 287

3 Answers3

3

I'm not sure what the name of this technique is called, but I know its pretty common in the mobile world these days (eg Twitter and Facebook apps use it). For practical purposes, the implementation would probably be a normal JTable that has a few modified components.

  1. Write your own TableModel to display the data (which is pretty easy to do), you can retrieve the information from anywhere, and add/remove data as you see fit. Refer to the documentation here . There are only a handful of methods that you need to implement

    The primary things that are important here are the getRowCount() method, which you would change as you add/remove data from the table, and the getValueAt() method which retrieves the data to display in the table.

    The TableModel would probably have an array-like object in it (such as ArrayList) that contains the data that should be displayed in the table. When you want to add/remove data from the table (ie the 'refresh'), you just add/remove rows from this array-list Object, then trigger a refresh on the table to pick up the change.

    Alternatively, if working with a database, you can have a database Cursor open in the TableModel that is called by getValueAt() whenever you want to display a value on the table.

  2. You would need to overwrite one of the GUI components such as the JScrollBar, or add a Listener to the scrollbar so that it detects when you try to scroll past the end of the table, and triggers the refresh.

  3. Visually, you would need to overwrite some of the painting components of the current Look and Feel if you want to display something special when you scroll past the end of the table (such as displaying the word "Refreshing"). You would also need to overwrite the JScrollBar if you want to add a few ticks to the 'height' of it, so that you can actually move the scrollbar into the 'refresh' part (just a display thing only - the Listeners above should do the actual refresh work)

wattostudios
  • 8,666
  • 13
  • 43
  • 57
2

This kind of technique is known as lazy loading. There are different approaches to make a JTable lazy.

Implementing a lazy table is not that easy as it seems, since you will be confronted with different loading states of the table. My prefered way to implement a lazy loading table (and little different then discribed by WATTO Studios) is to use/create a lazy loading list as tablemodel and using proxy objects to trigger loading of new data. So instead of loading your whole data when initialising your table, you only fill your table with proxy objects (empty data objects whereas only the original id is injected). Whenever a proxy object is been asked for any value (for example from a renderer, which will only happen, when the row gets visible), you fetch the original data object from your service - and maybe load the next 50 objects as well, to avoid asking your server for every single row - and replace the proxy objects with the new data.

On first sight, this looks kinda simple, but it can get really messy depending on your data. For example, when your data uses self referenced foreign keys on theirselfs and stuff like that. Moreover instead of gaining extra performance by loading data just when it is needed the table still often feels a bit slow, since it just loads the data when it is needed. So balancing performance and smooth scrolling behaviour is the challenge here, as well as filtering and sorting your data.

Using the keywords lazy loading, you should find some examples concerning this topic.

crusam
  • 6,140
  • 6
  • 40
  • 68
2
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • You always seem to have good examples for everything, don't you? :-) This one looks kinda interesting on first view too, especially to learn how a jtable works. – crusam Oct 23 '12 at 13:22