JTable delegates data storing and processing to its table data model. A table data model must implement the TableModel interface, which defines the methods for registering table model listeners, manipulating cells , and obtaining row count, column count, column class, and column name .
The AbstractTableModel class provides partial implementations for most of the methods in TableModel . It takes care of the management of listeners and provides some conveniences for generating TableModelEvents and dispatching them to the listeners. To create a concrete TableModel , you simply extend AbstractTableModel and implement at least the following three methods:
public int getRowCount()
public int getColumnCount()
public Object getValueAt( int row , int column )
The DefaultTableModel class extends AbstractTableModel and implements these three methods. Additionally, DefaultTableModel provides concrete storage for data. The data is stored in a vector. The elements in the vector are arrays of objects, each of which represents an individual cell value. The methods in DefaultTableModel for accessing and modifying data are shown in Figure 31.5.
Listing 31.3 gives an example that demonstrates table models. The example creates a table model (line 16), plugs the model to the table (line 20), appends a row to the table (line 25), inserts a row before the first row (line 26), removes a row with index 1 (line 28), adds a new column (line 29), and sets new values at specified cells (lines 30 “32). Figure 31.6 shows the output of the program.
1 import javax.swing.*; 2 import javax.swing.table.*; 3 4 public class TestTableModel extends JApplet { 5 // Create table column names 6 String[] columnNames = 7 { "Country" , "Capital" , "Population in Millions" , "Democracy" }; 8 9 // Create table data 10 Object[][] data = { 11 { "USA" , "Washington DC" , 280 , true }, 12 { "Canada" , "Ottawa" , 32 , true } 13 }; 14 15 // Create a model 16 DefaultTableModel tableModel = 17 new DefaultTableModel(data, columnNames); 18 19 // Create a table 20 JTable jTable1 = new JTable(tableModel); 21 22 public TestTableModel() { 23 add( new JScrollPane(jTable1)); 24 25 tableModel.addRow( new Object[]{ "France" , "Paris" , 60 , true }); 26 tableModel.insertRow( , new Object[] 27 { "India" , "New Delhi" , 1046 , true }); 28 tableModel.removeRow( 1 ); 29 tableModel.addColumn( "Area" ); 30 tableModel.setValueAt( 10 , , 4 ); 31 tableModel.setValueAt( 20 , 1 , 4 ); 32 tableModel.setValueAt( 30 , 2 , 4 ); 33 } 34 } |
TableModel manages table data. You can add and remove rows through a TableModel . You can also add a column through a TableModel . However, you cannot remove a column through a TableModel . To remove a column from a JTable , you have to use a table column model.
Table column models manage columns in a table. They can be used to select, add, move, and remove table columns . A table column model must implement the TableColumnModel interface, which defines the methods for registering table column model listeners, and for accessing and manipulating columns, as shown in Figure 31.7.
DefaultTableColumnModel is a concrete class that implements TableColumnModel and PropertyChangeListener . The DefaultTableColumnModel class stores its columns in a vector and contains an instance of ListSelectionModel for selecting columns.
The column model deals with all the columns in a table. The TableColumn class is used to model an individual column in the table. An instance of TableColumn for a specified column can be obtained using the getColumn(index) method in TableColumnModel or the getColumn(columnIdentifier) method in JTable .
Figure 31.8 shows the properties, constructors, and methods in TableColumn for manipulating column width and specifying the cell renderer, cell editor, and header renderer.
Listing 31.4 gives an example that demonstrates table column models. The example obtains the table column model from the table (line 21), moves the first column to the second (line 22), and removes the last column (lines 23). Figure 31.9 shows the output of the program.
1 import javax.swing.*; 2 import javax.swing.table.*; 3 4 public class TestTableColumnModel extends JApplet { 5 // Create table column names 6 String[] columnNames = 7 { "Country" , "Capital" , "Population in Millions" , "Democracy" }; 8 9 // Create table data 10 Object[][] data = { 11 { "USA" , "Washington DC" , 280 , true }, 12 { "Canada" , "Ottawa" , 32 , true } 13 }; 14 15 // Create a table 16 JTable jTable1 = new JTable(data, columnNames); 17 18 public TestTableColumnModel() { 19 add( new JScrollPane(jTable1)); 20 21 TableColumnModel columnModel = jTable1.getColumnModel(); 22 columnModel.moveColumn( , 1 ); 23 columnModel.removeColumn(columnModel.getColumn( 3 )); 24 } 25 } |
Note
Some of the methods defined in the table model and the table column model are also defined in the JTable class for convenience. For instance, the getColumnCount() method is defined in JTable , TableModel , and TableColumnModel , the addColumn method defined in the column model is also defined in the table model, and the getColumn() method defined in the column model is also defined in the JTable class. |
JTableHeader is a GUI component that manages the header of the JTable (see Figure 31.10). When you create a JTable , an instance of JTableHeader is automatically created and stored in the tableHeader property. By default, you can reorder the columns by dragging the header of the column. To disable it, set the reorderingAllowed property to false .