Home > Articles

  • Print
  • + Share This

Method Breakdown

The best way to understand how this intermediary TableSorter works is to walk through its source code. The basic idea of the TableSorter is that it knows and remembers which column is being sorted and the sort order. The TableSorter does not change the sort order inside of the table model, but instead keeps a separate reference. This allows the data to remain completely unchanged no matter how the user decides to sort it.

TableSorter():null

This is the base constructor that all the other constructors will call. This constructor initializes two inner classes that help with the handling of the sorting. These inner classes are detailed below.

TableSorter(TableModel):null

This constructor first calls the base constructor detailed above and then sets the local reference to the TableModel object that is passed in.

TableSorter(TableModel, JTableHeader):null

This constructor first calls the base constructor detailed above and then sets the local reference to the TableModel and JTableHeader that are passed in.

clearSortingState():void

This private method clears the sorting. This method is called only when the data or structure of the table has changed in some dramatic way. For instance, the adding of a column to the table model would cause this method to be called.

getTableModel():JTableModel

Simple getter method that returns a reference to my underlying table model. This is useful if I decide to retain a reference to the TableModel instead of my own table model, or if I did not retain a reference at all and need to get at it by drilling down from the JTable.

setTableModel(TableModel):void

Simple setter method that accepts a TableModel. This table model that gets passed in is the underlying model that supplies the data to the sorter. The TableSorter will attach a table listener to this supplied table model and notify the JTable that the table structure has changed.

getTableHeader():JTableHeader

Simple getter method that returns a reference to the JTableHeader that is being used by the JTable.

setTableHeader(JTableHeader):void

The TableSorter requires a reference to the JTableHeader so that it can attach a mouse listener to it. This setter method accepts that JTableHeader and attaches a mouse listener to it. These mouse events are what drive the sorting of the table.

isSorting():boolean

This method allows me to ask if the table is currently being sorted by one or more columns or if it is displaying a nonsorted state.

getDirective(int):Directive

A directive is an inner class that retains the state of a particular column. As the table is sorted, the TableSorter remembers which columns it is sorting by through directives. Each stored the index to a column and the direction that column is being stored in. This method retrieves a directive for a particular column. If there is no directive assigned to the requested column, a special directive is returned that has a column index of -1.

getSortingStatus(int):int

This method looks up the directive for a particular column and returns in what direction it is being sorted. By using constants, I can test against the integer that is being returned without having to worry about what integer means what.

sortingStatusChanged():void

A private method that cleans up all sorting. First, this method clears the sorting state. Next, it notifies all table listeners that the data has changed (to force the JTable to repaint), and finally it asked the table header to repaint itself.

setStoringStatus(int, int):void

In addition to being able to sort a column by clicking on it with the mouse, it is possible to instruct the TableSorter to sort a column programmatically. This method accepts a column and a direction. Again, the direction is referenced using constants defined in the TableSorter class.

getHeaderRendererIcon(int, int):Icon

The base implementation of the TableSorter class draws its own icon to signify sorting direction. This method will retrieve an icon that is the same as those used in the table header's display. Note that this creates a new instance of the icon, not just a reference to an existing icon.

cancelSorting():void

A private method that resets all sorting. First, this method removes all directives and then it calls sortingStatusChanged(), as described previously.

setColumnComparator(Class, Comparator):void

This method allows me to intervene and control how a column is being sorted. For instance, assume that column 0 in my underlying table model refers a custom object. This object would normally have its toString() method called. The output of this call is then compared using String's implementation of the Comparator interface. However, if I want to change how that is handled, I could write a custom class that extends the java.util.Comparator interface and tells the TableSorter to use it instead when comparing that type of object. The table sorter stores them in a java.util.Map.

getComparator(int):Comparator

This method returns the Comparator being used for a particular column.

getViewToModel():Row[]

This method returns the mapping of what the JTable contains compared to what is in the underlying TableModel. If the table is not currently being sorted, this mapping will be 1 to 1. Note that this method returns an array of Row objects that are defined below.

modelIndex(int):int

This method will return the index to a particular row from the underlying table model by returning the index of that row in the sorted TableSorter index. This allows me to manipulate the proper row when I would normally just call JTable.getSelectedRow(), for instance.

getModelToView():int[]

Similar to getViewToModel() listed above. This method returns an array of Row objects in the exact opposite mapping.

getRowCount():int

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

getColumnCount():int

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

getColumnName(int):String

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

getColumnClass(int):Class

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

isCellEditable(int, int):boolean

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

getValueAt(int, int):Object

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

setValueAt(Object, int, int):void

This method is implemented from the TableModel interface. It returns the results of the same method on the underlying TableModel.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.