Path: blob/master/src/demo/share/jfc/TableExample/OldJTable.java
41149 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.util.EventObject;42import java.util.List;43import javax.swing.JTable;44import javax.swing.table.DefaultTableModel;45import javax.swing.table.TableCellEditor;46import javax.swing.table.TableCellRenderer;47import javax.swing.table.TableColumn;484950/**51* The OldJTable is an unsupported class containing some methods that were52* deleted from the JTable between releases 0.6 and 0.753*/54@SuppressWarnings("serial")55public class OldJTable extends JTable56{57/*58* A new convenience method returning the index of the column in the59* co-ordinate space of the view.60*/61public int getColumnIndex(Object identifier) {62return getColumnModel().getColumnIndex(identifier);63}6465//66// Methods deleted from the JTable because they only work with the67// DefaultTableModel.68//6970public TableColumn addColumn(Object columnIdentifier, int width) {71return addColumn(columnIdentifier, width, null, null, null);72}7374public TableColumn addColumn(Object columnIdentifier, List<?> columnData) {75return addColumn(columnIdentifier, -1, null, null, columnData);76}7778// Override the new JTable implementation - it will not add a column to the79// DefaultTableModel.80public TableColumn addColumn(Object columnIdentifier, int width,81TableCellRenderer renderer,82TableCellEditor editor) {83return addColumn(columnIdentifier, width, renderer, editor, null);84}8586public TableColumn addColumn(Object columnIdentifier, int width,87TableCellRenderer renderer,88TableCellEditor editor, List<?> columnData) {89checkDefaultTableModel();9091// Set up the model side first92DefaultTableModel m = (DefaultTableModel)getModel();93m.addColumn(columnIdentifier, columnData.toArray());9495// The column will have been added to the end, so the index of the96// column in the model is the last element.97TableColumn newColumn = new TableColumn(98m.getColumnCount()-1, width, renderer, editor);99super.addColumn(newColumn);100return newColumn;101}102103// Not possilble to make this work the same way ... change it so that104// it does not delete columns from the model.105public void removeColumn(Object columnIdentifier) {106super.removeColumn(getColumn(columnIdentifier));107}108109public void addRow(Object[] rowData) {110checkDefaultTableModel();111((DefaultTableModel)getModel()).addRow(rowData);112}113114public void addRow(List<?> rowData) {115checkDefaultTableModel();116((DefaultTableModel)getModel()).addRow(rowData.toArray());117}118119public void removeRow(int rowIndex) {120checkDefaultTableModel();121((DefaultTableModel)getModel()).removeRow(rowIndex);122}123124public void moveRow(int startIndex, int endIndex, int toIndex) {125checkDefaultTableModel();126((DefaultTableModel)getModel()).moveRow(startIndex, endIndex, toIndex);127}128129public void insertRow(int rowIndex, Object[] rowData) {130checkDefaultTableModel();131((DefaultTableModel)getModel()).insertRow(rowIndex, rowData);132}133134public void insertRow(int rowIndex, List<?> rowData) {135checkDefaultTableModel();136((DefaultTableModel)getModel()).insertRow(rowIndex, rowData.toArray());137}138139public void setNumRows(int newSize) {140checkDefaultTableModel();141((DefaultTableModel)getModel()).setNumRows(newSize);142}143144public void setDataVector(Object[][] newData, List<?> columnIds) {145checkDefaultTableModel();146((DefaultTableModel)getModel()).setDataVector(147newData, columnIds.toArray());148}149150public void setDataVector(Object[][] newData, Object[] columnIds) {151checkDefaultTableModel();152((DefaultTableModel)getModel()).setDataVector(newData, columnIds);153}154155protected void checkDefaultTableModel() {156if(!(dataModel instanceof DefaultTableModel))157throw new InternalError("In order to use this method, the data model must be an instance of DefaultTableModel.");158}159160//161// Methods removed from JTable in the move from identifiers to ints.162//163164public Object getValueAt(Object columnIdentifier, int rowIndex) {165return super.getValueAt(rowIndex, getColumnIndex(columnIdentifier));166}167168public boolean isCellEditable(Object columnIdentifier, int rowIndex) {169return super.isCellEditable(rowIndex, getColumnIndex(columnIdentifier));170}171172public void setValueAt(Object aValue, Object columnIdentifier, int rowIndex) {173super.setValueAt(aValue, rowIndex, getColumnIndex(columnIdentifier));174}175176public boolean editColumnRow(Object identifier, int row) {177return super.editCellAt(row, getColumnIndex(identifier));178}179180public void moveColumn(Object columnIdentifier, Object targetColumnIdentifier) {181moveColumn(getColumnIndex(columnIdentifier),182getColumnIndex(targetColumnIdentifier));183}184185public boolean isColumnSelected(Object identifier) {186return isColumnSelected(getColumnIndex(identifier));187}188189public TableColumn addColumn(int modelColumn, int width) {190return addColumn(modelColumn, width, null, null);191}192193public TableColumn addColumn(int modelColumn) {194return addColumn(modelColumn, 75, null, null);195}196197/**198* Creates a new column with <I>modelColumn</I>, <I>width</I>,199* <I>renderer</I>, and <I>editor</I> and adds it to the end of200* the JTable's array of columns. This method also retrieves the201* name of the column using the model's <I>getColumnName(modelColumn)</I>202* method, and sets the both the header value and the identifier203* for this TableColumn accordingly.204* <p>205* The <I>modelColumn</I> is the index of the column in the model which206* will supply the data for this column in the table. This, like the207* <I>columnIdentifier</I> in previous releases, does not change as the208* columns are moved in the view.209* <p>210* For the rest of the JTable API, and all of its associated classes,211* columns are referred to in the co-ordinate system of the view, the212* index of the column in the model is kept inside the TableColumn213* and is used only to retrieve the information from the appropraite214* column in the model.215* <p>216*217* @param modelColumn The index of the column in the model218* @param width The new column's width. Or -1 to use219* the default width220* @param renderer The renderer used with the new column.221* Or null to use the default renderer.222* @param editor The editor used with the new column.223* Or null to use the default editor.224*/225public TableColumn addColumn(int modelColumn, int width,226TableCellRenderer renderer,227TableCellEditor editor) {228TableColumn newColumn = new TableColumn(229modelColumn, width, renderer, editor);230addColumn(newColumn);231return newColumn;232}233234//235// Methods that had their arguments switched.236//237238// These won't work with the new table package.239240/*241public Object getValueAt(int columnIndex, int rowIndex) {242return super.getValueAt(rowIndex, columnIndex);243}244245public boolean isCellEditable(int columnIndex, int rowIndex) {246return super.isCellEditable(rowIndex, columnIndex);247}248249public void setValueAt(Object aValue, int columnIndex, int rowIndex) {250super.setValueAt(aValue, rowIndex, columnIndex);251}252*/253254public boolean editColumnRow(int columnIndex, int rowIndex) {255return super.editCellAt(rowIndex, columnIndex);256}257258public boolean editColumnRow(int columnIndex, int rowIndex, EventObject e){259return super.editCellAt(rowIndex, columnIndex, e);260}261262263} // End Of Class OldJTable264265266