Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 137: Manage distributed JTables

Efficiently display huge JTables using distributed caching TableModels

  • Print
  • Feedback

Page 2 of 3

The implementation

A number of classes and interfaces were defined to implement this distributed caching JTable (Figure 1).

Figure 1. Class diagram of the classes implementing the distributed caching table model. Click on thumbnail to view full-size image.

A specialized TableModel called DistributedTableModel was written. This implementation satisfies the TableModel interface's methods by delegating all the hard work to the class DistributedTableClientCache, which retrieves data in bulk from the data source and caches it on the client side. DistributedTableClientCache retrieves all data from an object that implements the interface DistributedTableDataSource. This interface isolates all distributed data retrieval logic necessary to efficiently populate a TableModel in the manner we require. For example, the method getTableDescription() returns an object containing the table's descriptive elements—the number of rows and columns, and the column names and class types. The method Object[][] retrieveRows(int from, int to) throws Exception handles the actual data retrieval. Whenever the TableModel requires a row not in the cache at that time, the method retrieveRows() is called, and a set number of rows is retrieved.

To create an instance of a DistributedTableModel, the constructor is called with three parameters:

  • tableDataSource: An implementation of DistributedTableDataSource. It allows you to write a specialized version that, for example, retrieves its data from a Remote Method Invocation (RMI) or CORBA service.
  • chunkSize: The number of rows that should be retrieved at once from the server.
  • maximumCacheSize: The number of rows the cache should store before overwriting rows not required at the time.


Figure 2 shows a sequence diagram for this data retrieval process.

Figure 2. A sequence diagram shows DistributedTableModel's distributed data retrieval process when a JTable displays. Click on thumbnail to view full-size image.

Sorting

One common table requirement is the ability to sort columns in ascending or descending order. The JTable component does not provide built-in sorting functionality; it must be implemented in the TableModel implementation (see Resources). You must have all data on hand to implement a sort. Because all data is on the server side, the sort must occur there. This is easily done by sending a message to the server through the DistributedTableDataSource interface to sort the data on a particular column in an ascending or descending order. You can implement a manual sort, or a database can do your dirty work via the SQL ORDER BY operator. After the sort completes, the client-side data cache will be nullified, and the table will update. The JTable will immediately ask for the data to fill the part of the table currently in view. This will trigger a server-side fetch of the newly sorted data.

One problem presents itself, however, during the sorting process. What happens to users' row selections? Of course it's not acceptable to lose those selections. Selection is implemented in a JTable by the interface ListSelectionModel that registers the selected row indexes. If you change the underlying TableModel data by carrying out a sort, the ListSelectionModel will have no knowledge of the new selection indexes, and the selections will remain set to the old ones. However, you can perform a manual sort if you know the selected rows' new indexes. Before sorting, note the selected rows. After the sort, the table selections clear and then the selected rows' new indexes are set on the ListSelectionModel. Thus, the sort method in the interface DistributedTableDataSource takes the following form:

  • Print
  • Feedback

Resources