001/** 002 * Please feel free to use any fragment of the code in this file that you need in your own 003 * work. As far as I am concerned, it's in the public domain. No permission is necessary 004 * or required. Credit is always appreciated if you use a large chunk or base a 005 * significant product on one of my examples, but that's not required either. 006 * 007 * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 008 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 009 * PURPOSE. 010 * 011 * --- Joseph A. Huwaldt 012 */ 013package jahuwaldt.swing; 014 015import java.util.List; 016import java.util.Vector; 017import javax.swing.table.DefaultTableModel; 018 019/** 020 * A DefaultTableModel that has been modified to mark the cells as uneditable. 021 * 022 * <p> Modified by: Joseph A. Huwaldt </p> 023 * 024 * @author Joseph A. Huwaldt Date: January 29, 2014 025 * @version December 21, 2014 026 */ 027public class UneditableDefaultTableModel extends DefaultTableModel { 028 029 /** 030 * Constructs a default UneditableDefaultTableModel which is a table of zero columns 031 * and zero rows. 032 */ 033 public UneditableDefaultTableModel() { 034 } 035 036 /** 037 * Constructs a UneditableDefaultTableModel with rowCount and columnCount of null 038 * object values. 039 * 040 * @param rowCount the number of rows the table holds 041 * @param columnCount the number of columns the table holds 042 */ 043 public UneditableDefaultTableModel(int rowCount, int columnCount) { 044 super(rowCount, columnCount); 045 } 046 047 /** 048 * Constructs a UneditableDefaultTableModel with as many columns as there are elements 049 * in columnNames and rowCount of null object values. Each column's name will be taken 050 * from the columnNames vector. 051 * 052 * @param columnNames vector containing the names of the new columns; if this is null 053 * then the model has no columns 054 * @param rowCount the number of rows the table holds 055 */ 056 public UneditableDefaultTableModel(Vector columnNames, int rowCount) { 057 super(columnNames, rowCount); 058 } 059 060 /** 061 * Constructs a UneditableDefaultTableModel with as many columns as there are elements 062 * in columnNames and rowCount of null object values. Each column's name will be taken 063 * from the columnNames array. 064 * 065 * @param columnNames array containing the names of the new columns; if this is null 066 * then the model has no columns 067 * @param rowCount the number of rows the table holds 068 */ 069 public UneditableDefaultTableModel(Object[] columnNames, int rowCount) { 070 super(columnNames, rowCount); 071 } 072 073 /** 074 * Constructs a UneditableDefaultTableModel and initializes the table by passing data 075 * and columnNames to the setDataVector method. 076 * 077 * @param data the data of the table, a Vector of Vectors of Object values 078 * @param columnNames vector containing the names of the new columns 079 */ 080 public UneditableDefaultTableModel(Vector data, Vector columnNames) { 081 super(data, columnNames); 082 } 083 084 /** 085 * Constructs a UneditableDefaultTableModel and initializes the table by passing data 086 * and columnNames to the setDataVector method. 087 * 088 * @param data the data of the table, a List of Lists of Object values 089 * @param columnNames vector containing the names of the new columns 090 */ 091 public UneditableDefaultTableModel(List data, List columnNames) { 092 super(convertListOfListsToVector(data), convertListToVector(columnNames)); 093 } 094 095 private static Vector convertListToVector(List aList) { 096 Vector v = new Vector(); 097 v.addAll(aList); 098 return v; 099 } 100 101 private static Vector convertListOfListsToVector(List<List> aList) { 102 Vector v = new Vector(); 103 for (List row : aList) { 104 Vector rowV = new Vector(); 105 rowV.addAll(row); 106 v.add(rowV); 107 } 108 return v; 109 } 110 111 /** 112 * Constructs a DefaultTableModel and initializes the table by passing data and 113 * columnNames to the setDataVector method. The first index in the Object[][] array is 114 * the row index and the second is the column index. 115 * 116 * @param data the data of the table 117 * @param columnNames the names of the columns 118 */ 119 public UneditableDefaultTableModel(Object[][] data, Object[] columnNames) { 120 super(data, columnNames); 121 } 122 123 /** 124 * Returns false regardless of parameter values. 125 * 126 * @param row the row whose value is to be queried 127 * @param column the column whose value is to be queried 128 * @return Always returns false. 129 */ 130 @Override 131 public boolean isCellEditable(int row, int column) { 132 return false; 133 } 134 135}