| JTable Classes | TableCellRenderer Gotchas |
| Limitations | TableRowSorter |
| How JTables Work | Simple Example |
| Wide JTables | Real World Example |
| Useful Classes | Learning More |
| Strategy | Links |
// get information about all columns. final TableColumnModel columnModel = jTable.getColumnModel(); // setting column widths: columnModel.getColumn( col ).setPreferredWidth( widthInPixels ); columnModel.getColumn( col ).setMinWidth( widthInPixels ); columnModel.getColumn( col ).setMaxWidth( widthInPixels ); // adding a bit of extra space between the columns. columnModel.setColumnMargin( 5 );Note there is only one TableColumnModel object that serves all the columns.
JTabless are tricky. It is not that anything is that difficult, it is just that there are so many classes and so many methods to master. JTables are more like the plans for a do-it-yourself table than a table-creating appliance. I strongly suggest buying a textbook that covers them rather than trying to figure it out all on your own from the Javadoc.
| Classes and Interfaces Useful in Creating JTables | |
|---|---|
| Class | Use |
| AbstractTableModel | implements the TableModel with your own class to represent each row. |
| DefaultCellEditor | Implements TableCellEditor for some common data types. |
| DefaultTableCellRenderer | Implements TableCellRenderer for some common data types, including ImageIcon. |
| DefaultTableModel | implements the TableModel with each row represented by a Vector. |
| JTable | Represents the entire table. |
| JTableHeader | Represents all the header columns. |
| ListSelectionModel | Methods to track which rows are selected. |
| TableCellEditor | Methods to edit a single cell. |
| TableCellRenderer | Methods to display a single cell, both data and headers. |
| TableColumn | Represents one column, widths etc. Used to attach TableCellRenderers for both data and headers. |
| TableColumnModel | information about all columns. |
| TableModel | Represents the data in the table grid. |
| TableModelEvent | notify listeners that a table model has changed |
| TableRowSorter | Keep rows sorted. JDK 1..6+ only. |
// attaching a TableCellEditor to a column tableColumn.setCellEditor( new DefaultCellEditor( comboBox ) );where comboBox is a JComboBox scratch component the user is allowed to edit. DefaultCellEditor will handle popping up the scratch edit component, initialising it, and sending the new value to the TableModel. To do more elaborate edits, search for sample code on the web. If you apply colours and fonts to your scratch components, it will make it clearer to the user which cell he is editing. The crucial methods are:
| You can get the freshest copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) | |
| http://mindprod.com/jgloss/jtable.html | J:\mindprod\jgloss\jtable.html | |
![]() | ||
| Canadian Mind Products | ||
| mindprod.com IP:[65.110.21.43] | ||
| view Blog | Your face IP:[38.107.191.107] | |
| Feedback | You are visitor number 33,999. | |