Introduction to Java Programming-Comprehensive Version (6th Edition)

 

[Page 1065 ( continued )]

31.7. Table Model Events

JTable does not fire table events. It fires events like MouseEvent , KeyEvent , and ComponentEvent that are inherited from its superclass, JComponent . Table events are fired by table models, table column models, and table-selection models whenever changes are made to these models. Table models fire TableModelEvent when table data is changed. Table column models fire TableColumnModelEvent when columns are added, removed, or moved, or when the column selection changes. Table-selection models fire ListSelectionEvent when the selection changes.

To listen for these events, a listener must be registered with an appropriate model and implement the correct listener interface. Listing 31.10 gives an example that demonstrates how to use these events. The program displays messages on a text area when a row or a column is selected, when a cell is edited, or when a column is removed. Figure 31.15 is a sample run of the program.

Figure 31.15. Table event handlers display table events on a text area.


[Page 1066]
Listing 31.10. TableEventsDemo.java

(This item is displayed on pages 1066 - 1068 in the print version)

1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.event.*; 5 import javax.swing.table.*; 6 import java.util.*; 7 8 public class TableEventsDemo extends JApplet { 9 // Create table column names 10 private String[] columnNames = 11 { "Title" , "Copies Needed" , "Publisher" , "Date Published" , 12 "In-stock" , "Book Photo" }; 13 14 // Create image icons 15 private ImageIcon intro1eImageIcon = 16 new ImageIcon(getClass().getResource( "image/intro1e.gif" )); 17 private ImageIcon intro2eImageIcon = 18 new ImageIcon(getClass().getResource( "image/intro2e.gif" )); 19 private ImageIcon intro3eImageIcon = 20 new ImageIcon(getClass().getResource( "image/intro3e.jpg" )); 21 22 // Create table data 23 private Object[][] rowData = { 24 { "Introduction to Java Programming" , 120 , 25 "Que Education & Training" , 26 new GregorianCalendar( 1998 , 1-1 , 6 ).getTime(), 27 false , intro1eImageIcon}, 28 { "Introduction to Java Programming, 2E" , 220 , 29 "Que Education & Training" , 30 new GregorianCalendar( 1999 , 1-1 , 6 ).getTime(), 31 false , intro2eImageIcon}, 32 { "Introduction to Java Programming, 3E" , 220 , 33 "Prentice Hall" , 34 new GregorianCalendar( 2000 , 12-1 , ).getTime(), 35 true , intro3eImageIcon}, 36 }; 37 38 // Create a table model 39 private MyTableModel tableModel = new MyTableModel( 40 rowData, columnNames); 41 42 // Create a table 43 private JTable jTable1 = new JTable(tableModel); 44 45 // Get table column model 46 private TableColumnModel tableColumnModel = 47 jTable1.getColumnModel(); 48 49 // Get table selection model 50 private ListSelectionModel selectionModel = 51 jTable1.getSelectionModel(); 52 53 // Create a text area 54 private JTextArea jtaMessage = new JTextArea(); 55 56 // Create a button 57 private JButton jbtDeleteColumn = 58 new JButton( "Delete Selected Column" ); 59


[Page 1067]

60 public TableEventsDemo() { 61 // Set custom renderer for displaying images 62 TableColumn bookCover = jTable1.getColumn( "Book Photo" ); 63 bookCover.setCellRenderer( new MyImageCellRenderer()); 64 65 // Create a combo box for publishers 66 JComboBox jcboPublishers = new JComboBox(); 67 jcboPublishers.addItem( "Prentice Hall" ); 68 jcboPublishers.addItem( "Que Education & Training" ); 69 jcboPublishers.addItem( "McGraw-Hill" ); 70 71 // Set combo box as the editor for the publisher column 72 TableColumn publisherColumn = jTable1.getColumn( "Publisher" ); 73 publisherColumn.setCellEditor( 74 new DefaultCellEditor(jcboPublishers)); 75 76 jTable1.setRowHeight( 60 ); 77 jTable1.setColumnSelectionAllowed( true ); 78 79 JSplitPane jSplitPane1 = new JSplitPane( 80 JSplitPane.VERTICAL_SPLIT); 81 jSplitPane1.add( new JScrollPane(jTable1), JSplitPane.LEFT); 82 jSplitPane1.add( new JScrollPane(jtaMessage), JSplitPane.RIGHT); 83 add(jbtDeleteColumn, BorderLayout.NORTH); 84 add(jSplitPane1, BorderLayout.CENTER); 85 86 tableModel.addTableModelListener( new TableModelListener() { 87 public void tableChanged(TableModelEvent e) { 88 jtaMessage.append( "Table changed at row " + 89 e.getFirstRow() + " and column " + e.getColumn() + "\n" ); 90 } 91 }); 92 93 tableColumnModel.addColumnModelListener( 94 new TableColumnModelListener() { 95 public void columnRemoved(TableColumnModelEvent e) { 96 jtaMessage.append( "Column indexed at " + e.getFromIndex() + 97 " is deleted \n" ); 98 } 99 public void columnAdded(TableColumnModelEvent e) { 100 } 101 public void columnMoved(TableColumnModelEvent e) { 102 } 103 public void columnMarginChanged(ChangeEvent e) { 104 } 105 public void columnSelectionChanged(ListSelectionEvent e) { 106 } 107 }); 108 109 jbtDeleteColumn.addActionListener( new ActionListener() { 110 public void actionPerformed(ActionEvent e) { 111 if (jTable1.getSelectedColumn() >= ) { 112 TableColumnModel columnModel = jTable1.getColumnModel(); 113 TableColumn tableColumn = 114 columnModel.getColumn(jTable1.getSelectedColumn()); 115 columnModel.removeColumn(tableColumn); 116 } 117 } 118 }); 119


[Page 1068]

120 selectionModel.addListSelectionListener( 121 new ListSelectionListener() { 122 public void valueChanged(ListSelectionEvent e) { 123 jtaMessage.append( "Row " + jTable1.getSelectedRow() + 124 " and column " + jTable1.getSelectedColumn() + 125 " selected\n" ); 126 } 127 }); 128 } 129 }

To respond to the row and column selection events, you need to implement the valueChanged method in ListSelectionListener . To respond to the cell-editing event, you need to implement the tableChanged method in TableModelListener . To respond to the column-deletion event, you need to implement the columnRemoved method in TableColumnModelListener . Let's use the same table from the preceding example, but with a button added for deleting the selected column and a text area for displaying the messages.

A table model is created using MyTableModel (lines 39 “40), which was given in Listing 31.6. When a table is created (line 43), its default column model and selection model are also created. Therefore, you can obtain the table column model and selection model from the table (lines 46 “51).

When a row or a column is selected, a ListSelectionEvent is fired by selectionModel , which invokes the handler to display the selected row and column in the text area (lines 120 “127). When the content or structure of the table is changed, a TableModelEvent is fired by tableModel , which invokes the handler to display the last row and last column of the changed data in the text area (lines 86 “91). When a column is deleted by clicking the Delete Selected Column button, a ColumnModelEvent is fired by tableColumnModel , which invokes the handler to display the index of the deleted column (lines 93 “107).

 

Категории