Introduction to Java Programming-Comprehensive Version (6th Edition)
31.4. Case Study: Modifying Rows and Columns
This case study demonstrates the use of table models, table column models, list-selection models, and the TableColumn class. The program allows the user to choose selection mode and selection type, the add or remove rows and columns, and save, clear, or restore the table, as shown in Figure 31.11(a).
Figure 31.11. You can add, remove, and modify rows and columns in a table interactively.
The Add New Row button adds a new empty row before the currently selected row, as shown in Figure 31.11(b). If no row is currently selected, a new empty row is appended to the end of the table.
When you click the Add New Column button, an input dialog box is displayed to receive the title of the column, as shown in Figure 31.12(a). The new column is appended in the table, as shown in Figure 31.12(b).
Figure 31.12. You can add a new column in a table.
(This item is displayed on page 1056 in the print version)
The Delete Selected Row button deletes the first selected row. The Delete Selected Column button deletes the first selected column.
The Save button saves the current table data and column names . The Clear button clears the row data in the table. The Restore button restores the save table.
Listing 31.5 gives the program.
Listing 31.5. TableModelDemo.java
(This item is displayed on pages 1056 - 1059 in the print version)
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.table.*; 5 import java.io.*; 6 import java.util.Vector; 7 8 public class TableModelDemo extends JApplet { 9 // Create table column names 10 private String[] columnNames = 11 { "Country" , "Capital" , "Population in Millions" , "Democracy" }; 12 13 // Create table data 14 private Object[][] rowData = { 15 { "USA" , "Washington DC" , 280 , true }, 16 { "Canada" , "Ottawa" , 32 , true }, 17 { "United Kingdom" , "London" , 60 , true }, 18 { "Germany" , "Berlin" , 83 , true }, 19 { "France" , "Paris" , 60 , true }, 20 { "Norway" , "Oslo" , 4.5 , true }, 21 { "India" , "New Deli" , 1046 , true } 22 }; 23 24 // Create a table model 25 private DefaultTableModel tableModel = new DefaultTableModel( 26 rowData, columnNames); 27 28 // Create a table 29 private JTable jTable1 = new JTable(tableModel); 30 31 // Create buttons 32 private JButton jbtAddRow = new JButton( "Add New Row" ); 33 private JButton jbtAddColumn = new JButton( "Add New Column" ); 34 private JButton jbtDeleteRow = new JButton( "Delete Selected Row" ); 35 private JButton jbtDeleteColumn = new JButton( 36 "Delete Selected Column" ); 37 private JButton jbtSave = new JButton( "Save" ); 38 private JButton jbtClear = new JButton( "Clear" ); 39 private JButton jbtRestore = new JButton( "Restore" ); 40 41 // Create a combo box for selection modes 42 private JComboBox jcboSelectionMode = 43 new JComboBox( new String[] { "SINGLE_SELECTION" , 44 "SINGLE_INTERVAL_SELECTION" , "MULTIPLE_INTERVAL_SELECTION" }); 45 46 // Create check boxes 47 private JCheckBox jchkRowSelectionAllowed = 48 new JCheckBox( "RowSelectionAllowed" , true ); 49 private JCheckBox jchkColumnSelectionAllowed = 50 new JCheckBox( "ColumnSelectionAllowed" , false ); 51 52 public TableModelDemo() { 53 JPanel panel1 = new JPanel(); 54 panel1.setLayout( new GridLayout( 2 , 2 )); 55 panel1.add(jbtAddRow); 56 panel1.add(jbtAddColumn); 57 panel1.add(jbtDeleteRow); 58 panel1.add(jbtDeleteColumn); 59 60 JPanel panel2 = new JPanel(); 61 panel2.add(jbtSave); 62 panel2.add(jbtClear); 63 panel2.add(jbtRestore); 64 65 JPanel panel3 = new JPanel(); 66 panel3.setLayout( new BorderLayout( 5 , )); 67 panel3.add( new JLabel( "Selection Mode" ), BorderLayout.WEST); 68 panel3.add(jcboSelectionMode, BorderLayout.CENTER); 69 70 JPanel panel4 = new JPanel(); 71 panel4.setLayout( new FlowLayout(FlowLayout.LEFT)); 72 panel4.add(jchkRowSelectionAllowed); 73 panel4.add(jchkColumnSelectionAllowed); 74 75 JPanel panel5 = new JPanel(); 76 panel5.setLayout( new GridLayout( 2 , 1 )); 77 panel5.add(panel3); 78 panel5.add(panel4); 79 80 JPanel panel6 = new JPanel(); 81 panel6.setLayout( new BorderLayout()); 82 panel6.add(panel1, BorderLayout.SOUTH); 83 panel6.add(panel2, BorderLayout.CENTER); 84 85 add(panel5, BorderLayout.NORTH); 86 add( new JScrollPane(jTable1) , 87 BorderLayout.CENTER); 88 add(panel6, BorderLayout.SOUTH); 89 90 // Initialize table selection mode 91 jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 92 93 jbtAddRow.addActionListener( new ActionListener() { 94 public void actionPerformed(ActionEvent e) { 95 if ( jTable1.getSelectedRow() >= ) 96 tableModel.insertRow (jTable1.getSelectedRow() , 97 new java.util.Vector()); 98 else 99 tableModel.addRow( new java.util.Vector()); 100 } 101 }); 102 103 jbtAddColumn.addActionListener( new ActionListener() { 104 public void actionPerformed(ActionEvent e) { 105 String name = JOptionPane.showInputDialog( "New Column Name" ); 106 tableModel.addColumn(name, new java.util.Vector()); 107 } 108 }); 109 110 jbtDeleteRow.addActionListener( new ActionListener() { 111 public void actionPerformed(ActionEvent e) { 112 if ( jTable1.getSelectedRow() >= ) 113 tableModel.removeRow(jTable1.getSelectedRow()); 114 } 115 }); 116 117 jbtDeleteColumn.addActionListener( new ActionListener() { 118 public void actionPerformed(ActionEvent e) { 119 if ( jTable1.getSelectedColumn() >= ) { 120 TableColumnModel columnModel = jTable1.getColumnModel(); 121 TableColumn tableColumn = 122 columnModel.getColumn(jTable1.getSelectedColumn()); 123 columnModel.removeColumn(tableColumn); 124 } 125 } 126 }); 127 128 jbtSave.addActionListener( new ActionListener() { 129 public void actionPerformed(ActionEvent e) { 130 try { 131 ObjectOutputStream out = new ObjectOutputStream( 132 new FileOutputStream( "tablemodel.dat" )); 133 out.writeObject(tableModel.getDataVector()); 134 out.writeObject(getColumnNames()); 135 out.close(); 136 } 137 catch (Exception ex) { 138 ex.printStackTrace(); 139 } 140 } 141 }); 142 143 jbtClear.addActionListener( new ActionListener() { 144 public void actionPerformed(ActionEvent e) { 145 tableModel.setRowCount( ); 146 } 147 }); 148 149 jbtRestore.addActionListener( new ActionListener() { 150 public void actionPerformed(ActionEvent e) { 151 try { 152 ObjectInputStream in = new ObjectInputStream( 153 new FileInputStream( "tablemodel.dat" )); 154 Vector rowData = (Vector)in.readObject(); 155 Vector columnNames = (Vector)in.readObject(); 156 tableModel.setDataVector(rowData, columnNames); 157 in.close(); 158 } 159 catch (Exception ex) { 160 ex.printStackTrace(); 161 } 162 } 163 }); 164 165 jchkRowSelectionAllowed.addActionListener( new ActionListener() { 166 public void actionPerformed(ActionEvent e) { 167 jTable1.setRowSelectionAllowed ( 168 jchkRowSelectionAllowed.isSelected()); 169 } 170 }); 171 172 jchkColumnSelectionAllowed.addActionListener( 173 new ActionListener() { 174 public void actionPerformed(ActionEvent e) { 175 jTable1.setColumnSelectionAllowed ( 176 jchkColumnSelectionAllowed.isSelected()); 177 } 178 }); 179 180 jcboSelectionMode.addActionListener( new ActionListener() { 181 public void actionPerformed(ActionEvent e) { 182 String selectedItem = 183 (String) jcboSelectionMode.getSelectedItem(); 184 185 if (selectedItem.equals( "SINGLE_SELECTION" )) 186 jTable1.setSelectionMode ( 187 ListSelectionModel.SINGLE_SELECTION); 188 else if (selectedItem.equals( "SINGLE_INTERVAL_SELECTION" )) 189 jTable1.setSelectionMode( 190 ListSelectionModel.SINGLE_INTERVAL_SELECTION); 191 else if (selectedItem.equals( "MULTIPLE_INTERVAL_SELECTION" )) 192 jTable1.setSelectionMode( 193 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 194 } 195 }); 196 } 197 198 private Vector getColumnNames() { 199 Vector<String> columnNames = new Vector<String>(); 200 201 for ( int i = ; i < jTable1.getColumnCount(); i++) 202 columnNames.add( jTable1.getColumnName(i) ); 203 204 return columnNames; 205 } 206 }
|
A table model is created using DefaultTableModel with row data and column names (lines 25 “26). This model is used to create a JTable (line 29).
The GUI objects (buttons, combo box, check boxes) are created in lines 32 “50 and are placed in the UI in lines 53 “88.
The table-selection mode is the same as the list-selection mode. By default, the selection mode is MULTIPLE_INTERVAL_SELECTION . To match the initial value in the selection combo box ( jcboSelectionMode ), the table's selection mode is set to SINGLE_SELECTION .
The Add New Row button action is processed in lines 93 “101. The insertRow method inserts a new row before the selected row (lines 95 “96). If no row is currently selected, the addRow method appends a new row into the table model (line 99).
The Add New Column button action is processed in lines 103 “108. The addColumn method appends a new column into the table model (line 106).
The Delete Selected Row button action is processed in lines 110 “115. The removeRow(rowIndex) method removes the selected row from the table model (line 113).
The Delete Selected Column button action is processed in lines 117 “126. To remove a column, you have to use the removeColumn method in TableColumnModel (line 123).
The Save button action is processed in lines 128 “141. It writes row data and column names to an output file using object stream (lines 133 “134). The column names are obtained using the getColumnNames() method (lines 198 “205). You may attempt to save tableModel , because tableModel is an instance of DefaultTableModel (lines 25 “26) and DefaultTableModel is serializable. However, tableModel may contain non-serializable listeners for TableModel event.
The Clear button action is processed in lines 143 “147. It clears the table by setting the row count to 0 (line 145).
The Restore button action is processed in lines 149 “163. It reads row data and column names from the file using object stream (lines 154 “155), and sets the new data and column names to the table model (line 156).