Usage with Java 5

This library was initially developed targeting Java 5, and then modified to handle the new filtering capabilities in Java 6. Both versions were very different, but considering that the features in Java 6 will likely stay, I decided to retrofit the new implementation into the Java 5 ' library, adding some classes that only exist on the Java 6 standard library. These added classes are placed in the net.coderazzi.filters.artifacts package (only for the Java 5 implementation).

At the end, both versions share most of the code, although the internal imports are different in some cases. And users see mostly the same interface using Java 5 or Java 6, which is an important feature, in case some later migration happens. Specific exceptions exist; for example, autoselection or adaptive choices are only available on the Java 6 version.

There is, however, a difference between JTable users in Java 5 and Java 6: the former does not provide sorting capabilities. The solution is usually to setup a table model with sorting capabilities, like the TableSorter. The source version of TableFilter includes such class for the test programs, although the TableFilter itself does obviously not require it.

Although this library exports the same interface to Java 5 and 6 users, the internal implementation is quite different; in Java 6, there is one table model, while in Java 5 users must handle at least two table models (plus a third one if sorting is also added):

  • The normal table model associated initially to the table.
  • A model providing the filtering capabilities.

This library is distributed with an implementation of the second model, described below, and it can be setup automatically; note, however, that the usage of multiple table models makes some operations more complicated. For example, once that filtering is on place, the row number in the view does not correspond any more to the row number in the model; the JTable provides methods to enquiry for translations between both views, but in version 5, users must extract the model and perform these queries against the model.

In practical terms, this means that the user must perform multiple model conversions; for example, the provided example includes the following code to obtain the selected row element:

int selected = table.getSelectedRow();
if (selected != -1) {
  int model = ((TableModelFilter) table.getModel()).convertRowIndexToModel(selected);
  Object selection = tableModel.getRow(tableSorter.modelIndex(model));

Note that from version 5.0.0, it is not released anymore the Java 5 version (Java 5 entered its end-of-life on April 8, 2008 and is no longer supported as of November 3, 2009). The last release including the Java 5 compatible version is numbered 4.5.0 (17th November 2012).


Java 5 users of this library must attach, to the table, a model implementing the interface ITableModelFilter

If the attached model does not implement this interface, the library automatically creates one and replaces the current model with it. The default implementation is called TableModelFilter.

This implementation tries to mimic the behaviour found in the Java 6 tables; however, users can definitely provide their own implementation, for performance or extended interface reasons. The implementation provides two specific methods to convert positions between the index and the model: