Suggestions for porting to the new JTable API ============================================= The complete list of API changes since the last release is given in APIChanges.txt. Most of these changes have resulted in methods with differently typed arguments so re-compiling any code you have with the new Swing classes will highlight most of the differences. See the APIChanges.txt file or documentation for the new format for the methods. Unfortunately, some methods were changed in a way that preserved the types of the arguments but changed what they meant. Here is a list of he methods that need to be checked after you have a clean compile: 1. JTable's getColumn(Object identifier) method When columns are submitted to the JTable, either automatically or with the addColumn() methods, the JTable sets default values for the the identifiers of the new columns, and uses the column names for the default values. In the previous release they were taken from the default implementation in the AbstractTableModel which created Integer objects for each column. To locate columns in the new scheme, using identifiers, use the name of the column or use the TableColumn's setIdentifier() method to set the identifiers of the columns to the keys you want to use. For example, the demonstration program TableExample4 has been changed as follows: FROM: firstNameColumn = tableView.getColumn(new Integer(0)); TO: firstNameColumn = tableView.getColumn("First Name"); The getColumn() method will throw an exception when it can't find the identifier passed to it, so it should be easy to locate any problems caused by the change in the defaults. 2. getValueAt(), setValueAt(), isCellEditable() in JTable These methods were defined with arguments in (row, column) order in the TableModel, AbstractTableModel and DefaultTableModel but with the arguments in (column, row) order in the JTable. If you used these methods in the JTable, remember to switch the arguments. The addColumn() methods in the JTable, and the DefaultTableModel ---------------------------------------------------------------- All of the addColumn(...) wrapper methods in the JTable have been deleted except the main addColumn(TableColumn) method. A few releases ago we added a feature to the JTable that made it very easy to set up a JTable with data from a model. This feature is controlled with a flag: autoCreateColumnsFromModel which should normally be left on (the default). In this release, default renderers and editors are set up automatically based on the type of the objects in the column (as returned by the getColumnType() method in the TableModel interface). The JTable now comes with default renderers and editors for columns with types: Object, Number and Boolean so that when the JTable sets up a default view of a table model it will display and be editable without special configuration. The easiest way to use the JTable is to build your model of the data first, then submit it to the JTable using: JTable view = new JTable(dataModel); For a more complete example, see the start of the API docs for the JTable. In this release, the default renderers and editors can be changed by using the setDefaultRenderer() methods in the JTable. If, after doing this, you still need to submit special renderers or editors for particular columns you can modify the TableColumn attributes to replace the JTable's default renderers. The example program TableExample4 in the example/demo area gives a complete example of doing this. In the 0.5 release, the addColumn() methods in the JTable used "instanceof" to detect that their model was a DefaultTableModel. When it was, these cover methods served two purposes: they added data to the model, and then added TableColumns (describing the way that data should be displayed) to the view. All of the addColumn() methods that relied on "instanceof" have been removed in this release and the JTable now only ever uses methods on the model which are specified in the TableModel interface. This has made the DefaultTableModel less useful than it was in the previous release and we now recommend subclassing AbstractTableModel (or wrapping your data in the methods of the TableModel interface) rather than copying the data into the DefaultTableModel column by column. If you want to submit columns in a different order to the order the columns appear in the model you can use the addColumn() method in the JTable as follows: JTable table = new JTable(); table.setAutoCreateColumnsFromModel(false); table.setModel(myData); table.addColumn(new TableColumn(17)) // Show column 17 first. table.addColumn(new TableColumn(11, 100)) // Column 11, width 100. table.addColumn(new TableColumn(3, 100, myRenderer, myEditor)) ... etc. If you do want to use the DefaultTableModel, be sure to set its identifiers before submitting it to the JTable because the JTable will use them to set the names in its header when addColumn() is called in the JTable. If you already have code that submitted columns of data to the JTable sequentially and you don't want to change it, it is still possible to deal with this though eventually you will probably want to upgrade all this sort of code with something more general. You need to add the columns to the DefaultTableModel rather than to the view - like this: DefaultTableModel model = new DefaultTableModel(); model.addColumn("Names", myArrayOfNames); model.addColumn("Ages", myArrayOfAges); JTable table = new JTable(model); You can also add the columns to the model after it has been added to the view, but this will be slower and will clear any specific renderers you may have already set. A subclass of the new JTable implementation, called OldJTable, containing some of the methods in the JTable that were deleted because of their dependency on the DefaulTableModel has been provided as a porting aid. The source is in the demo area though it will not be provided in the releases after this one.