1 /* 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 42 import java.util.EventObject; 43 import java.util.List; 44 import javax.swing.JTable; 45 import javax.swing.table.DefaultTableModel; 46 import javax.swing.table.TableCellEditor; 47 import javax.swing.table.TableCellRenderer; 48 import javax.swing.table.TableColumn; 49 50 51 /** 52 * The OldJTable is an unsupported class containing some methods that were 53 * deleted from the JTable between releases 0.6 and 0.7 54 */ 55 @SuppressWarnings("serial") 56 public class OldJTable extends JTable 57 { 58 /* 59 * A new convenience method returning the index of the column in the 60 * co-ordinate space of the view. 61 */ getColumnIndex(Object identifier)62 public int getColumnIndex(Object identifier) { 63 return getColumnModel().getColumnIndex(identifier); 64 } 65 66 // 67 // Methods deleted from the JTable because they only work with the 68 // DefaultTableModel. 69 // 70 addColumn(Object columnIdentifier, int width)71 public TableColumn addColumn(Object columnIdentifier, int width) { 72 return addColumn(columnIdentifier, width, null, null, null); 73 } 74 addColumn(Object columnIdentifier, List columnData)75 public TableColumn addColumn(Object columnIdentifier, List columnData) { 76 return addColumn(columnIdentifier, -1, null, null, columnData); 77 } 78 79 // Override the new JTable implementation - it will not add a column to the 80 // DefaultTableModel. addColumn(Object columnIdentifier, int width, TableCellRenderer renderer, TableCellEditor editor)81 public TableColumn addColumn(Object columnIdentifier, int width, 82 TableCellRenderer renderer, 83 TableCellEditor editor) { 84 return addColumn(columnIdentifier, width, renderer, editor, null); 85 } 86 addColumn(Object columnIdentifier, int width, TableCellRenderer renderer, TableCellEditor editor, List columnData)87 public TableColumn addColumn(Object columnIdentifier, int width, 88 TableCellRenderer renderer, 89 TableCellEditor editor, List columnData) { 90 checkDefaultTableModel(); 91 92 // Set up the model side first 93 DefaultTableModel m = (DefaultTableModel)getModel(); 94 m.addColumn(columnIdentifier, columnData.toArray()); 95 96 // The column will have been added to the end, so the index of the 97 // column in the model is the last element. 98 TableColumn newColumn = new TableColumn( 99 m.getColumnCount()-1, width, renderer, editor); 100 super.addColumn(newColumn); 101 return newColumn; 102 } 103 104 // Not possilble to make this work the same way ... change it so that 105 // it does not delete columns from the model. removeColumn(Object columnIdentifier)106 public void removeColumn(Object columnIdentifier) { 107 super.removeColumn(getColumn(columnIdentifier)); 108 } 109 addRow(Object[] rowData)110 public void addRow(Object[] rowData) { 111 checkDefaultTableModel(); 112 ((DefaultTableModel)getModel()).addRow(rowData); 113 } 114 addRow(List rowData)115 public void addRow(List rowData) { 116 checkDefaultTableModel(); 117 ((DefaultTableModel)getModel()).addRow(rowData.toArray()); 118 } 119 removeRow(int rowIndex)120 public void removeRow(int rowIndex) { 121 checkDefaultTableModel(); 122 ((DefaultTableModel)getModel()).removeRow(rowIndex); 123 } 124 moveRow(int startIndex, int endIndex, int toIndex)125 public void moveRow(int startIndex, int endIndex, int toIndex) { 126 checkDefaultTableModel(); 127 ((DefaultTableModel)getModel()).moveRow(startIndex, endIndex, toIndex); 128 } 129 insertRow(int rowIndex, Object[] rowData)130 public void insertRow(int rowIndex, Object[] rowData) { 131 checkDefaultTableModel(); 132 ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData); 133 } 134 insertRow(int rowIndex, List rowData)135 public void insertRow(int rowIndex, List rowData) { 136 checkDefaultTableModel(); 137 ((DefaultTableModel)getModel()).insertRow(rowIndex, rowData.toArray()); 138 } 139 setNumRows(int newSize)140 public void setNumRows(int newSize) { 141 checkDefaultTableModel(); 142 ((DefaultTableModel)getModel()).setNumRows(newSize); 143 } 144 setDataVector(Object[][] newData, List columnIds)145 public void setDataVector(Object[][] newData, List columnIds) { 146 checkDefaultTableModel(); 147 ((DefaultTableModel)getModel()).setDataVector( 148 newData, columnIds.toArray()); 149 } 150 setDataVector(Object[][] newData, Object[] columnIds)151 public void setDataVector(Object[][] newData, Object[] columnIds) { 152 checkDefaultTableModel(); 153 ((DefaultTableModel)getModel()).setDataVector(newData, columnIds); 154 } 155 checkDefaultTableModel()156 protected void checkDefaultTableModel() { 157 if(!(dataModel instanceof DefaultTableModel)) 158 throw new InternalError("In order to use this method, the data model must be an instance of DefaultTableModel."); 159 } 160 161 // 162 // Methods removed from JTable in the move from identifiers to ints. 163 // 164 getValueAt(Object columnIdentifier, int rowIndex)165 public Object getValueAt(Object columnIdentifier, int rowIndex) { 166 return super.getValueAt(rowIndex, getColumnIndex(columnIdentifier)); 167 } 168 isCellEditable(Object columnIdentifier, int rowIndex)169 public boolean isCellEditable(Object columnIdentifier, int rowIndex) { 170 return super.isCellEditable(rowIndex, getColumnIndex(columnIdentifier)); 171 } 172 setValueAt(Object aValue, Object columnIdentifier, int rowIndex)173 public void setValueAt(Object aValue, Object columnIdentifier, int rowIndex) { 174 super.setValueAt(aValue, rowIndex, getColumnIndex(columnIdentifier)); 175 } 176 editColumnRow(Object identifier, int row)177 public boolean editColumnRow(Object identifier, int row) { 178 return super.editCellAt(row, getColumnIndex(identifier)); 179 } 180 moveColumn(Object columnIdentifier, Object targetColumnIdentifier)181 public void moveColumn(Object columnIdentifier, Object targetColumnIdentifier) { 182 moveColumn(getColumnIndex(columnIdentifier), 183 getColumnIndex(targetColumnIdentifier)); 184 } 185 isColumnSelected(Object identifier)186 public boolean isColumnSelected(Object identifier) { 187 return isColumnSelected(getColumnIndex(identifier)); 188 } 189 addColumn(int modelColumn, int width)190 public TableColumn addColumn(int modelColumn, int width) { 191 return addColumn(modelColumn, width, null, null); 192 } 193 addColumn(int modelColumn)194 public TableColumn addColumn(int modelColumn) { 195 return addColumn(modelColumn, 75, null, null); 196 } 197 198 /** 199 * Creates a new column with <I>modelColumn</I>, <I>width</I>, 200 * <I>renderer</I>, and <I>editor</I> and adds it to the end of 201 * the JTable's array of columns. This method also retrieves the 202 * name of the column using the model's <I>getColumnName(modelColumn)</I> 203 * method, and sets the both the header value and the identifier 204 * for this TableColumn accordingly. 205 * <p> 206 * The <I>modelColumn</I> is the index of the column in the model which 207 * will supply the data for this column in the table. This, like the 208 * <I>columnIdentifier</I> in previous releases, does not change as the 209 * columns are moved in the view. 210 * <p> 211 * For the rest of the JTable API, and all of its associated classes, 212 * columns are referred to in the co-ordinate system of the view, the 213 * index of the column in the model is kept inside the TableColumn 214 * and is used only to retrieve the information from the appropraite 215 * column in the model. 216 * <p> 217 * 218 * @param modelColumn The index of the column in the model 219 * @param width The new column's width. Or -1 to use 220 * the default width 221 * @param renderer The renderer used with the new column. 222 * Or null to use the default renderer. 223 * @param editor The editor used with the new column. 224 * Or null to use the default editor. 225 */ addColumn(int modelColumn, int width, TableCellRenderer renderer, TableCellEditor editor)226 public TableColumn addColumn(int modelColumn, int width, 227 TableCellRenderer renderer, 228 TableCellEditor editor) { 229 TableColumn newColumn = new TableColumn( 230 modelColumn, width, renderer, editor); 231 addColumn(newColumn); 232 return newColumn; 233 } 234 235 // 236 // Methods that had their arguments switched. 237 // 238 239 // These won't work with the new table package. 240 241 /* 242 public Object getValueAt(int columnIndex, int rowIndex) { 243 return super.getValueAt(rowIndex, columnIndex); 244 } 245 246 public boolean isCellEditable(int columnIndex, int rowIndex) { 247 return super.isCellEditable(rowIndex, columnIndex); 248 } 249 250 public void setValueAt(Object aValue, int columnIndex, int rowIndex) { 251 super.setValueAt(aValue, rowIndex, columnIndex); 252 } 253 */ 254 editColumnRow(int columnIndex, int rowIndex)255 public boolean editColumnRow(int columnIndex, int rowIndex) { 256 return super.editCellAt(rowIndex, columnIndex); 257 } 258 editColumnRow(int columnIndex, int rowIndex, EventObject e)259 public boolean editColumnRow(int columnIndex, int rowIndex, EventObject e){ 260 return super.editCellAt(rowIndex, columnIndex, e); 261 } 262 263 264 } // End Of Class OldJTable 265