Predefined renderers and editors are convenient and easy to use, but their functions are limited. The predefined image icon renderer displays the image icon in a label. The image icon cannot be scaled. If you want the whole image to fit in a cell , you need to create a custom renderer.
A custom renderer can be created by extending DefaultTableCellRenderer , which is a default implementation for the TableCellRenderer interface. The custom renderer must override the getTableCellRendererComponent method to return a component for rendering the table cell. The getTableCellRendererComponent method is defined as follows :
public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean isFocused, int row, int column)
This method signature is very similar to the getListCellRendererComponent method used to create custom list cell renderers.
This method is passed with a JTable , the value associated with the cell, information regarding whether the value is selected and the cell has the focus, and the row and column indices of the value. The component returned from the method is painted on the cell in the table. The class in Listing 31.8, MyImageCellRenderer , creates a renderer for displaying image icons in a panel.
1 import javax.swing.*; 2 import javax.swing.table.*; 3 import java.awt.*; 4 5 public class MyImageCellRenderer extends DefaultTableCellRenderer { 6 /** Override this method in DefaultTableCellRenderer */ 7 public Component getTableCellRendererComponent 8 (JTable table, Object value, boolean isSelected, 9 boolean isFocused, int row, int column) { 10 Image image = ((ImageIcon)value).getImage(); 11 ImageViewer imageViewer = new ImageViewer(image); 12 return imageViewer; 13 } 14 } |
You can also create a custom editor. JTable provides the DefaultCellEditor class, which can be used to edit a cell in a text field, a check box, or a combo box. To use it, simply create a text field, a check box, or a combo box, and pass it to DefaultCellEditor 's constructor to create an editor.
Using a custom renderer and editor, the preceding example can be revised to display scaled images and to use a custom combo editor to edit the cells in the Publisher column, as shown in Figure 31.14. The program is given in Listing 31.9.
1 import java.awt.*; 2 import javax.swing.*; 3 import javax.swing.table.*; 4 import java.util.*; 5 6 public class CustomTableCellRenderEditorDemo extends JApplet { 7 // Create table column names 8 private String[] columnNames = 9 { "Title" , "Copies Needed" , "Publisher" , "Date Published" , 10 "In-stock" , "Book Photo" }; 11 12 // Create image icons 13 private ImageIcon intro1eImageIcon = 14 new ImageIcon(getClass().getResource( "image/intro1e.gif" )); 15 private ImageIcon intro2eImageIcon = 16 new ImageIcon(getClass().getResource( "image/intro2e.gif" )); 17 private ImageIcon intro3eImageIcon = 18 new ImageIcon(getClass().getResource( "image/intro3e.jpg" )); 19 20 // Create table data 21 private Object[][] rowData = { 22 { "Introduction to Java Programming" , 120 , 23 "Que Education & Training" , 24 new GregorianCalendar( 1998 , 1-1 , 6 ).getTime(), 25 false , intro1eImageIcon}, 26 { "Introduction to Java Programming, 2E" , 220 , 27 "Que Education & Training" , 28 new GregorianCalendar( 1999 , 1-1 , 6 ).getTime(), 29 false , intro2eImageIcon}, 30 { "Introduction to Java Programming, 3E" , 220 , 31 "Prentice Hall" , 32 new GregorianCalendar( 2000 , 12-1 , ).getTime(), 33 true , intro3eImageIcon}, 34 }; 35 36 // Create a table model 37 private MyTableModel tableModel = new MyTableModel( 38 rowData, columnNames); 39 40 // Create a table 41 private JTable jTable1 = new JTable(tableModel); 42 43 public CustomTableCellRenderEditorDemo() { 44 // Set custom renderer for displaying images 45 TableColumn bookCover = jTable1.getColumn( "Book Photo" ); 46 bookCover.setCellRenderer( new MyImageCellRenderer()); 47 48 // Create a combo box for publishers 49 JComboBox jcboPublishers = new JComboBox(); 50 jcboPublishers.addItem( "Prentice Hall" ); 51 jcboPublishers.addItem( "Que Education & Training" ); 52 jcboPublishers.addItem( "McGraw-Hill" ); 53 54 // Set combo box as the editor for the publisher column 55 TableColumn publisherColumn = jTable1.getColumn( "Publisher" ); 56 publisherColumn.setCellEditor( 57 new DefaultCellEditor(jcboPublishers)); 58 59 jTable1.setRowHeight( 60 ); 60 add( new JScrollPane(jTable1) , 61 BorderLayout.CENTER); 62 } 63 } |
This example uses the same table model ( MyTableModel ) that was created in the preceding example (lines 37 “38). By default, image icons are displayed using the predefined image icon renderer. To use MyImageCellRenderer to display the image, you have to explicitly specify the MyImageCellRenderer renderer for the Book Photo column (line 46). Likewise, you have to explicitly specify the combo box editor for the Publisher column (lines 56 “57); otherwise the default editor would be used.
When you edit a cell in the Publisher column, a combo box of three items is displayed. When you select an item from the box, it is displayed in the cell. You did not write the code for handling selections. The selections are handled by the DefaultCellEditor class.
When you resize the Book Photo column, the image is resized to fit into the whole cell. With the predefined image renderer, you can only see part of the image if the cell is smaller than the image.