In addition to the TableModel implementation, there are several helper classes that the TableModel uses to handle the sorting and separation of the sorting from the data. A description of their purposes is detailed in the following sections.
The Row inner class represents the mapping from the TableSorter to the underlying table model. A Row object knows which row it references in the underlying table model and also handles comparisons when a column is being sorted.
This inner class handles table events that are being received from the underlying table model. Normally, the JTable listens to the events from the table model and acts on them. However, because the Table Sorter is now between these two objects, it intercepts the messages from the underlying table model and decides which messages to pass on to the JTable. A common situation that is particularly interesting is when the underlying table model sends out a message that the table structure has changed. When that occurs, the TableSorter resets all its sorting and goes back to displaying the data in its original form.
This inner class handles mouse events from the JTable's header. When the user clicks the header, this class receives those messages and sorts the table appropriately.
This inner class implements the Icon interface, which is a rather simple class that draws a basic arrow denoting the direction of the sorting. If I want to provide a fancier arrow or another shape, I would change this class.
Similar to the cell renderer that I have discussed in previous articles, this class handles the rendering of a column's header. If a column is being sorted, this class returns a JLabel that has both the column name (retrieved from the underlying table model) and an instance of the Arrow class detailed previously. If the column is not being sorted, a JLabel with only the column name will be returned.
A directive is basically a structure that remembers in which direction a column is being sorted.
Sun has made it very simple to add sorting to any JTable without affecting an existing TableModel. Even a DefaultTablelModel can be used with the TableSorter. Good luck!