53 * of all JavaBeans™
54 * has been added to the <code>java.beans</code> package.
55 * Please see {@link java.beans.XMLEncoder}.
56 *
57 * @author Philip Milne
58 *
59 * @see TableModel
60 * @see #getDataVector
61 */
62 @SuppressWarnings("serial") // Same-version serialization only
63 public class DefaultTableModel extends AbstractTableModel implements Serializable {
64
65 //
66 // Instance Variables
67 //
68
69 /**
70 * The <code>Vector</code> of <code>Vectors</code> of
71 * <code>Object</code> values.
72 */
73 protected Vector<Vector<Object>> dataVector;
74
75 /** The <code>Vector</code> of column identifiers. */
76 protected Vector<Object> columnIdentifiers;
77
78 //
79 // Constructors
80 //
81
82 /**
83 * Constructs a default <code>DefaultTableModel</code>
84 * which is a table of zero columns and zero rows.
85 */
86 public DefaultTableModel() {
87 this(0, 0);
88 }
89
90 private static <E> Vector<E> newVector(int size) {
91 Vector<E> v = new Vector<>(size);
92 v.setSize(size);
93 return v;
94 }
95
96 /**
104 * @see #setValueAt
105 */
106 public DefaultTableModel(int rowCount, int columnCount) {
107 this(newVector(columnCount), rowCount);
108 }
109
110 /**
111 * Constructs a <code>DefaultTableModel</code> with as many columns
112 * as there are elements in <code>columnNames</code>
113 * and <code>rowCount</code> of <code>null</code>
114 * object values. Each column's name will be taken from
115 * the <code>columnNames</code> vector.
116 *
117 * @param columnNames <code>vector</code> containing the names
118 * of the new columns; if this is
119 * <code>null</code> then the model has no columns
120 * @param rowCount the number of rows the table holds
121 * @see #setDataVector
122 * @see #setValueAt
123 */
124 public DefaultTableModel(Vector<Object> columnNames, int rowCount) {
125 setDataVector(newVector(rowCount), columnNames);
126 }
127
128 /**
129 * Constructs a <code>DefaultTableModel</code> with as many
130 * columns as there are elements in <code>columnNames</code>
131 * and <code>rowCount</code> of <code>null</code>
132 * object values. Each column's name will be taken from
133 * the <code>columnNames</code> array.
134 *
135 * @param columnNames <code>array</code> containing the names
136 * of the new columns; if this is
137 * <code>null</code> then the model has no columns
138 * @param rowCount the number of rows the table holds
139 * @see #setDataVector
140 * @see #setValueAt
141 */
142 public DefaultTableModel(Object[] columnNames, int rowCount) {
143 this(convertToVector(columnNames), rowCount);
144 }
145
146 /**
147 * Constructs a <code>DefaultTableModel</code> and initializes the table
148 * by passing <code>data</code> and <code>columnNames</code>
149 * to the <code>setDataVector</code> method.
150 *
151 * @param data the data of the table, a <code>Vector</code>
152 * of <code>Vector</code>s of <code>Object</code>
153 * values
154 * @param columnNames <code>vector</code> containing the names
155 * of the new columns
156 * @see #getDataVector
157 * @see #setDataVector
158 */
159 public DefaultTableModel(Vector<Vector<Object>> data, Vector<Object> columnNames) {
160 setDataVector(data, columnNames);
161 }
162
163 /**
164 * Constructs a <code>DefaultTableModel</code> and initializes the table
165 * by passing <code>data</code> and <code>columnNames</code>
166 * to the <code>setDataVector</code>
167 * method. The first index in the <code>Object[][]</code> array is
168 * the row index and the second is the column index.
169 *
170 * @param data the data of the table
171 * @param columnNames the names of the columns
172 * @see #getDataVector
173 * @see #setDataVector
174 */
175 public DefaultTableModel(Object[][] data, Object[] columnNames) {
176 setDataVector(data, columnNames);
177 }
178
179 /**
180 * Returns the <code>Vector</code> of <code>Vectors</code>
181 * that contains the table's
182 * data values. The vectors contained in the outer vector are
183 * each a single row of values. In other words, to get to the cell
184 * at row 1, column 5: <p>
185 *
186 * <code>((Vector)getDataVector().elementAt(1)).elementAt(5);</code>
187 *
188 * @return the vector of vectors containing the tables data values
189 *
190 * @see #newDataAvailable
191 * @see #newRowsAdded
192 * @see #setDataVector
193 */
194 public Vector<Vector<Object>> getDataVector() {
195 return dataVector;
196 }
197
198 private static <E> Vector<E> nonNullVector(Vector<E> v) {
199 return (v != null) ? v : new Vector<>();
200 }
201
202 /**
203 * Replaces the current <code>dataVector</code> instance variable
204 * with the new <code>Vector</code> of rows, <code>dataVector</code>.
205 * Each row is represented in <code>dataVector</code> as a
206 * <code>Vector</code> of <code>Object</code> values.
207 * <code>columnIdentifiers</code> are the names of the new
208 * columns. The first name in <code>columnIdentifiers</code> is
209 * mapped to column 0 in <code>dataVector</code>. Each row in
210 * <code>dataVector</code> is adjusted to match the number of
211 * columns in <code>columnIdentifiers</code>
212 * either by truncating the <code>Vector</code> if it is too long,
213 * or adding <code>null</code> values if it is too short.
214 * <p>Note that passing in a <code>null</code> value for
215 * <code>dataVector</code> results in unspecified behavior,
216 * an possibly an exception.
217 *
218 * @param dataVector the new data vector
219 * @param columnIdentifiers the names of the columns
220 * @see #getDataVector
221 */
222 public void setDataVector(Vector<Vector<Object>> dataVector,
223 Vector<Object> columnIdentifiers) {
224 this.dataVector = nonNullVector(dataVector);
225 this.columnIdentifiers = nonNullVector(columnIdentifiers);
226 justifyRows(0, getRowCount());
227 fireTableStructureChanged();
228 }
229
230 /**
231 * Replaces the value in the <code>dataVector</code> instance
232 * variable with the values in the array <code>dataVector</code>.
233 * The first index in the <code>Object[][]</code>
234 * array is the row index and the second is the column index.
235 * <code>columnIdentifiers</code> are the names of the new columns.
236 *
237 * @param dataVector the new data vector
238 * @param columnIdentifiers the names of the columns
239 * @see #setDataVector(Vector, Vector)
240 */
241 public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) {
242 setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers));
243 }
244
250 */
251 public void newDataAvailable(TableModelEvent event) {
252 fireTableChanged(event);
253 }
254
255 //
256 // Manipulating rows
257 //
258
259 private void justifyRows(int from, int to) {
260 // Sometimes the DefaultTableModel is subclassed
261 // instead of the AbstractTableModel by mistake.
262 // Set the number of rows for the case when getRowCount
263 // is overridden.
264 dataVector.setSize(getRowCount());
265
266 for (int i = from; i < to; i++) {
267 if (dataVector.elementAt(i) == null) {
268 dataVector.setElementAt(new Vector<>(), i);
269 }
270 ((Vector)dataVector.elementAt(i)).setSize(getColumnCount());
271 }
272 }
273
274 /**
275 * Ensures that the new rows have the correct number of columns.
276 * This is accomplished by using the <code>setSize</code> method in
277 * <code>Vector</code> which truncates vectors
278 * which are too long, and appends <code>null</code>s if they
279 * are too short.
280 * This method also sends out a <code>tableChanged</code>
281 * notification message to all the listeners.
282 *
283 * @param e this <code>TableModelEvent</code> describes
284 * where the rows were added.
285 * If <code>null</code> it assumes
286 * all the rows were newly added
287 * @see #getDataVector
288 */
289 public void newRowsAdded(TableModelEvent e) {
290 justifyRows(e.getFirstRow(), e.getLastRow() + 1);
357 /**
358 * Adds a row to the end of the model. The new row will contain
359 * <code>null</code> values unless <code>rowData</code> is specified.
360 * Notification of the row being added will be generated.
361 *
362 * @param rowData optional data of the row being added
363 */
364 public void addRow(Object[] rowData) {
365 addRow(convertToVector(rowData));
366 }
367
368 /**
369 * Inserts a row at <code>row</code> in the model. The new row
370 * will contain <code>null</code> values unless <code>rowData</code>
371 * is specified. Notification of the row being added will be generated.
372 *
373 * @param row the row index of the row to be inserted
374 * @param rowData optional data of the row being added
375 * @exception ArrayIndexOutOfBoundsException if the row was invalid
376 */
377 public void insertRow(int row, Vector<Object> rowData) {
378 dataVector.insertElementAt(rowData, row);
379 justifyRows(row, row+1);
380 fireTableRowsInserted(row, row);
381 }
382
383 /**
384 * Inserts a row at <code>row</code> in the model. The new row
385 * will contain <code>null</code> values unless <code>rowData</code>
386 * is specified. Notification of the row being added will be generated.
387 *
388 * @param row the row index of the row to be inserted
389 * @param rowData optional data of the row being added
390 * @exception ArrayIndexOutOfBoundsException if the row was invalid
391 */
392 public void insertRow(int row, Object[] rowData) {
393 insertRow(row, convertToVector(rowData));
394 }
395
396 private static int gcd(int i, int j) {
397 return (j == 0) ? i : gcd(j, i%j);
467 fireTableRowsDeleted(row, row);
468 }
469
470 //
471 // Manipulating columns
472 //
473
474 /**
475 * Replaces the column identifiers in the model. If the number of
476 * <code>newIdentifier</code>s is greater than the current number
477 * of columns, new columns are added to the end of each row in the model.
478 * If the number of <code>newIdentifier</code>s is less than the current
479 * number of columns, all the extra columns at the end of a row are
480 * discarded.
481 *
482 * @param columnIdentifiers vector of column identifiers. If
483 * <code>null</code>, set the model
484 * to zero columns
485 * @see #setNumRows
486 */
487 public void setColumnIdentifiers(Vector<Object> columnIdentifiers) {
488 setDataVector(dataVector, columnIdentifiers);
489 }
490
491 /**
492 * Replaces the column identifiers in the model. If the number of
493 * <code>newIdentifier</code>s is greater than the current number
494 * of columns, new columns are added to the end of each row in the model.
495 * If the number of <code>newIdentifier</code>s is less than the current
496 * number of columns, all the extra columns at the end of a row are
497 * discarded.
498 *
499 * @param newIdentifiers array of column identifiers.
500 * If <code>null</code>, set
501 * the model to zero columns
502 * @see #setNumRows
503 */
504 public void setColumnIdentifiers(Object[] newIdentifiers) {
505 setColumnIdentifiers(convertToVector(newIdentifiers));
506 }
507
508 /**
533 *
534 * @param columnName the identifier of the column being added
535 */
536 public void addColumn(Object columnName) {
537 addColumn(columnName, (Vector<Object>)null);
538 }
539
540 /**
541 * Adds a column to the model. The new column will have the
542 * identifier <code>columnName</code>, which may be null.
543 * <code>columnData</code> is the
544 * optional vector of data for the column. If it is <code>null</code>
545 * the column is filled with <code>null</code> values. Otherwise,
546 * the new data will be added to model starting with the first
547 * element going to row 0, etc. This method will send a
548 * <code>tableChanged</code> notification message to all the listeners.
549 *
550 * @param columnName the identifier of the column being added
551 * @param columnData optional data of the column being added
552 */
553 public void addColumn(Object columnName, Vector<Object> columnData) {
554 columnIdentifiers.addElement(columnName);
555 if (columnData != null) {
556 int columnSize = columnData.size();
557 if (columnSize > getRowCount()) {
558 dataVector.setSize(columnSize);
559 }
560 justifyRows(0, getRowCount());
561 int newColumn = getColumnCount() - 1;
562 for(int i = 0; i < columnSize; i++) {
563 Vector<Object> row = dataVector.elementAt(i);
564 row.setElementAt(columnData.elementAt(i), newColumn);
565 }
566 }
567 else {
568 justifyRows(0, getRowCount());
569 }
570
571 fireTableStructureChanged();
572 }
635 * @param row the row whose value is to be queried
636 * @param column the column whose value is to be queried
637 * @return true
638 * @see #setValueAt
639 */
640 public boolean isCellEditable(int row, int column) {
641 return true;
642 }
643
644 /**
645 * Returns an attribute value for the cell at <code>row</code>
646 * and <code>column</code>.
647 *
648 * @param row the row whose value is to be queried
649 * @param column the column whose value is to be queried
650 * @return the value Object at the specified cell
651 * @exception ArrayIndexOutOfBoundsException if an invalid row or
652 * column was given
653 */
654 public Object getValueAt(int row, int column) {
655 Vector<Object> rowVector = dataVector.elementAt(row);
656 return rowVector.elementAt(column);
657 }
658
659 /**
660 * Sets the object value for the cell at <code>column</code> and
661 * <code>row</code>. <code>aValue</code> is the new value. This method
662 * will generate a <code>tableChanged</code> notification.
663 *
664 * @param aValue the new value; this can be null
665 * @param row the row whose value is to be changed
666 * @param column the column whose value is to be changed
667 * @exception ArrayIndexOutOfBoundsException if an invalid row or
668 * column was given
669 */
670 public void setValueAt(Object aValue, int row, int column) {
671 Vector<Object> rowVector = dataVector.elementAt(row);
672 rowVector.setElementAt(aValue, column);
673 fireTableCellUpdated(row, column);
674 }
675
676 //
677 // Protected Methods
678 //
679
680 /**
681 * Returns a vector that contains the same objects as the array.
682 * @param anArray the array to be converted
683 * @return the new vector; if <code>anArray</code> is <code>null</code>,
684 * returns <code>null</code>
685 */
686 protected static Vector<Object> convertToVector(Object[] anArray) {
687 if (anArray == null) {
688 return null;
689 }
690 Vector<Object> v = new Vector<>(anArray.length);
691 for (Object o : anArray) {
692 v.addElement(o);
693 }
694 return v;
695 }
696
697 /**
698 * Returns a vector of vectors that contains the same objects as the array.
699 * @param anArray the double array to be converted
700 * @return the new vector of vectors; if <code>anArray</code> is
701 * <code>null</code>, returns <code>null</code>
702 */
703 protected static Vector<Vector<Object>> convertToVector(Object[][] anArray) {
704 if (anArray == null) {
705 return null;
706 }
707 Vector<Vector<Object>> v = new Vector<>(anArray.length);
708 for (Object[] o : anArray) {
709 v.addElement(convertToVector(o));
710 }
711 return v;
712 }
713
714 } // End of class DefaultTableModel
|
53 * of all JavaBeans™
54 * has been added to the <code>java.beans</code> package.
55 * Please see {@link java.beans.XMLEncoder}.
56 *
57 * @author Philip Milne
58 *
59 * @see TableModel
60 * @see #getDataVector
61 */
62 @SuppressWarnings("serial") // Same-version serialization only
63 public class DefaultTableModel extends AbstractTableModel implements Serializable {
64
65 //
66 // Instance Variables
67 //
68
69 /**
70 * The <code>Vector</code> of <code>Vectors</code> of
71 * <code>Object</code> values.
72 */
73 @SuppressWarnings("rawtypes")
74 protected Vector<Vector> dataVector;
75
76 /** The <code>Vector</code> of column identifiers. */
77 @SuppressWarnings("rawtypes")
78 protected Vector columnIdentifiers;
79 // Unfortunately, for greater source compatibility the inner-most
80 // Vector in the two fields above is being left raw. The Vector is
81 // read as well as written so using Vector<?> is not suitable and
82 // using Vector<Object> (without adding copying of input Vectors),
83 // would disallow existing code that used, say, a Vector<String>
84 // as an input parameter.
85
86 //
87 // Constructors
88 //
89
90 /**
91 * Constructs a default <code>DefaultTableModel</code>
92 * which is a table of zero columns and zero rows.
93 */
94 public DefaultTableModel() {
95 this(0, 0);
96 }
97
98 private static <E> Vector<E> newVector(int size) {
99 Vector<E> v = new Vector<>(size);
100 v.setSize(size);
101 return v;
102 }
103
104 /**
112 * @see #setValueAt
113 */
114 public DefaultTableModel(int rowCount, int columnCount) {
115 this(newVector(columnCount), rowCount);
116 }
117
118 /**
119 * Constructs a <code>DefaultTableModel</code> with as many columns
120 * as there are elements in <code>columnNames</code>
121 * and <code>rowCount</code> of <code>null</code>
122 * object values. Each column's name will be taken from
123 * the <code>columnNames</code> vector.
124 *
125 * @param columnNames <code>vector</code> containing the names
126 * of the new columns; if this is
127 * <code>null</code> then the model has no columns
128 * @param rowCount the number of rows the table holds
129 * @see #setDataVector
130 * @see #setValueAt
131 */
132 public DefaultTableModel(Vector<?> columnNames, int rowCount) {
133 setDataVector(newVector(rowCount), columnNames);
134 }
135
136 /**
137 * Constructs a <code>DefaultTableModel</code> with as many
138 * columns as there are elements in <code>columnNames</code>
139 * and <code>rowCount</code> of <code>null</code>
140 * object values. Each column's name will be taken from
141 * the <code>columnNames</code> array.
142 *
143 * @param columnNames <code>array</code> containing the names
144 * of the new columns; if this is
145 * <code>null</code> then the model has no columns
146 * @param rowCount the number of rows the table holds
147 * @see #setDataVector
148 * @see #setValueAt
149 */
150 public DefaultTableModel(Object[] columnNames, int rowCount) {
151 this(convertToVector(columnNames), rowCount);
152 }
153
154 /**
155 * Constructs a <code>DefaultTableModel</code> and initializes the table
156 * by passing <code>data</code> and <code>columnNames</code>
157 * to the <code>setDataVector</code> method.
158 *
159 * @param data the data of the table, a <code>Vector</code>
160 * of <code>Vector</code>s of <code>Object</code>
161 * values
162 * @param columnNames <code>vector</code> containing the names
163 * of the new columns
164 * @see #getDataVector
165 * @see #setDataVector
166 */
167 public DefaultTableModel(Vector<Vector<?>> data, Vector<?> columnNames) {
168 setDataVector(data, columnNames);
169 }
170
171 /**
172 * Constructs a <code>DefaultTableModel</code> and initializes the table
173 * by passing <code>data</code> and <code>columnNames</code>
174 * to the <code>setDataVector</code>
175 * method. The first index in the <code>Object[][]</code> array is
176 * the row index and the second is the column index.
177 *
178 * @param data the data of the table
179 * @param columnNames the names of the columns
180 * @see #getDataVector
181 * @see #setDataVector
182 */
183 public DefaultTableModel(Object[][] data, Object[] columnNames) {
184 setDataVector(data, columnNames);
185 }
186
187 /**
188 * Returns the <code>Vector</code> of <code>Vectors</code>
189 * that contains the table's
190 * data values. The vectors contained in the outer vector are
191 * each a single row of values. In other words, to get to the cell
192 * at row 1, column 5: <p>
193 *
194 * <code>((Vector)getDataVector().elementAt(1)).elementAt(5);</code>
195 *
196 * @return the vector of vectors containing the tables data values
197 *
198 * @see #newDataAvailable
199 * @see #newRowsAdded
200 * @see #setDataVector
201 */
202 @SuppressWarnings("rawtypes")
203 public Vector<Vector> getDataVector() {
204 return dataVector;
205 }
206
207 private static <E> Vector<E> nonNullVector(Vector<E> v) {
208 return (v != null) ? v : new Vector<>();
209 }
210
211 /**
212 * Replaces the current <code>dataVector</code> instance variable
213 * with the new <code>Vector</code> of rows, <code>dataVector</code>.
214 * Each row is represented in <code>dataVector</code> as a
215 * <code>Vector</code> of <code>Object</code> values.
216 * <code>columnIdentifiers</code> are the names of the new
217 * columns. The first name in <code>columnIdentifiers</code> is
218 * mapped to column 0 in <code>dataVector</code>. Each row in
219 * <code>dataVector</code> is adjusted to match the number of
220 * columns in <code>columnIdentifiers</code>
221 * either by truncating the <code>Vector</code> if it is too long,
222 * or adding <code>null</code> values if it is too short.
223 * <p>Note that passing in a <code>null</code> value for
224 * <code>dataVector</code> results in unspecified behavior,
225 * an possibly an exception.
226 *
227 * @param dataVector the new data vector
228 * @param columnIdentifiers the names of the columns
229 * @see #getDataVector
230 */
231 @SuppressWarnings({"rawtypes", "unchecked"})
232 public void setDataVector(Vector<Vector<?>> dataVector,
233 Vector<?> columnIdentifiers) {
234 this.dataVector = nonNullVector((Vector<Vector>)((Vector)dataVector));
235 this.columnIdentifiers = nonNullVector(columnIdentifiers);
236 justifyRows(0, getRowCount());
237 fireTableStructureChanged();
238 }
239
240 /**
241 * Replaces the value in the <code>dataVector</code> instance
242 * variable with the values in the array <code>dataVector</code>.
243 * The first index in the <code>Object[][]</code>
244 * array is the row index and the second is the column index.
245 * <code>columnIdentifiers</code> are the names of the new columns.
246 *
247 * @param dataVector the new data vector
248 * @param columnIdentifiers the names of the columns
249 * @see #setDataVector(Vector, Vector)
250 */
251 public void setDataVector(Object[][] dataVector, Object[] columnIdentifiers) {
252 setDataVector(convertToVector(dataVector), convertToVector(columnIdentifiers));
253 }
254
260 */
261 public void newDataAvailable(TableModelEvent event) {
262 fireTableChanged(event);
263 }
264
265 //
266 // Manipulating rows
267 //
268
269 private void justifyRows(int from, int to) {
270 // Sometimes the DefaultTableModel is subclassed
271 // instead of the AbstractTableModel by mistake.
272 // Set the number of rows for the case when getRowCount
273 // is overridden.
274 dataVector.setSize(getRowCount());
275
276 for (int i = from; i < to; i++) {
277 if (dataVector.elementAt(i) == null) {
278 dataVector.setElementAt(new Vector<>(), i);
279 }
280 dataVector.elementAt(i).setSize(getColumnCount());
281 }
282 }
283
284 /**
285 * Ensures that the new rows have the correct number of columns.
286 * This is accomplished by using the <code>setSize</code> method in
287 * <code>Vector</code> which truncates vectors
288 * which are too long, and appends <code>null</code>s if they
289 * are too short.
290 * This method also sends out a <code>tableChanged</code>
291 * notification message to all the listeners.
292 *
293 * @param e this <code>TableModelEvent</code> describes
294 * where the rows were added.
295 * If <code>null</code> it assumes
296 * all the rows were newly added
297 * @see #getDataVector
298 */
299 public void newRowsAdded(TableModelEvent e) {
300 justifyRows(e.getFirstRow(), e.getLastRow() + 1);
367 /**
368 * Adds a row to the end of the model. The new row will contain
369 * <code>null</code> values unless <code>rowData</code> is specified.
370 * Notification of the row being added will be generated.
371 *
372 * @param rowData optional data of the row being added
373 */
374 public void addRow(Object[] rowData) {
375 addRow(convertToVector(rowData));
376 }
377
378 /**
379 * Inserts a row at <code>row</code> in the model. The new row
380 * will contain <code>null</code> values unless <code>rowData</code>
381 * is specified. Notification of the row being added will be generated.
382 *
383 * @param row the row index of the row to be inserted
384 * @param rowData optional data of the row being added
385 * @exception ArrayIndexOutOfBoundsException if the row was invalid
386 */
387 public void insertRow(int row, Vector<?> rowData) {
388 dataVector.insertElementAt(rowData, row);
389 justifyRows(row, row+1);
390 fireTableRowsInserted(row, row);
391 }
392
393 /**
394 * Inserts a row at <code>row</code> in the model. The new row
395 * will contain <code>null</code> values unless <code>rowData</code>
396 * is specified. Notification of the row being added will be generated.
397 *
398 * @param row the row index of the row to be inserted
399 * @param rowData optional data of the row being added
400 * @exception ArrayIndexOutOfBoundsException if the row was invalid
401 */
402 public void insertRow(int row, Object[] rowData) {
403 insertRow(row, convertToVector(rowData));
404 }
405
406 private static int gcd(int i, int j) {
407 return (j == 0) ? i : gcd(j, i%j);
477 fireTableRowsDeleted(row, row);
478 }
479
480 //
481 // Manipulating columns
482 //
483
484 /**
485 * Replaces the column identifiers in the model. If the number of
486 * <code>newIdentifier</code>s is greater than the current number
487 * of columns, new columns are added to the end of each row in the model.
488 * If the number of <code>newIdentifier</code>s is less than the current
489 * number of columns, all the extra columns at the end of a row are
490 * discarded.
491 *
492 * @param columnIdentifiers vector of column identifiers. If
493 * <code>null</code>, set the model
494 * to zero columns
495 * @see #setNumRows
496 */
497 public void setColumnIdentifiers(Vector<?> columnIdentifiers) {
498 setDataVector((Vector<Vector<?>>)dataVector, columnIdentifiers);
499 }
500
501 /**
502 * Replaces the column identifiers in the model. If the number of
503 * <code>newIdentifier</code>s is greater than the current number
504 * of columns, new columns are added to the end of each row in the model.
505 * If the number of <code>newIdentifier</code>s is less than the current
506 * number of columns, all the extra columns at the end of a row are
507 * discarded.
508 *
509 * @param newIdentifiers array of column identifiers.
510 * If <code>null</code>, set
511 * the model to zero columns
512 * @see #setNumRows
513 */
514 public void setColumnIdentifiers(Object[] newIdentifiers) {
515 setColumnIdentifiers(convertToVector(newIdentifiers));
516 }
517
518 /**
543 *
544 * @param columnName the identifier of the column being added
545 */
546 public void addColumn(Object columnName) {
547 addColumn(columnName, (Vector<Object>)null);
548 }
549
550 /**
551 * Adds a column to the model. The new column will have the
552 * identifier <code>columnName</code>, which may be null.
553 * <code>columnData</code> is the
554 * optional vector of data for the column. If it is <code>null</code>
555 * the column is filled with <code>null</code> values. Otherwise,
556 * the new data will be added to model starting with the first
557 * element going to row 0, etc. This method will send a
558 * <code>tableChanged</code> notification message to all the listeners.
559 *
560 * @param columnName the identifier of the column being added
561 * @param columnData optional data of the column being added
562 */
563 @SuppressWarnings("unchecked") // Adding element to raw columnIdentifiers
564 public void addColumn(Object columnName, Vector<Object> columnData) {
565 columnIdentifiers.addElement(columnName);
566 if (columnData != null) {
567 int columnSize = columnData.size();
568 if (columnSize > getRowCount()) {
569 dataVector.setSize(columnSize);
570 }
571 justifyRows(0, getRowCount());
572 int newColumn = getColumnCount() - 1;
573 for(int i = 0; i < columnSize; i++) {
574 Vector<Object> row = dataVector.elementAt(i);
575 row.setElementAt(columnData.elementAt(i), newColumn);
576 }
577 }
578 else {
579 justifyRows(0, getRowCount());
580 }
581
582 fireTableStructureChanged();
583 }
646 * @param row the row whose value is to be queried
647 * @param column the column whose value is to be queried
648 * @return true
649 * @see #setValueAt
650 */
651 public boolean isCellEditable(int row, int column) {
652 return true;
653 }
654
655 /**
656 * Returns an attribute value for the cell at <code>row</code>
657 * and <code>column</code>.
658 *
659 * @param row the row whose value is to be queried
660 * @param column the column whose value is to be queried
661 * @return the value Object at the specified cell
662 * @exception ArrayIndexOutOfBoundsException if an invalid row or
663 * column was given
664 */
665 public Object getValueAt(int row, int column) {
666 return dataVector.elementAt(row).elementAt(column);
667 }
668
669 /**
670 * Sets the object value for the cell at <code>column</code> and
671 * <code>row</code>. <code>aValue</code> is the new value. This method
672 * will generate a <code>tableChanged</code> notification.
673 *
674 * @param aValue the new value; this can be null
675 * @param row the row whose value is to be changed
676 * @param column the column whose value is to be changed
677 * @exception ArrayIndexOutOfBoundsException if an invalid row or
678 * column was given
679 */
680 @SuppressWarnings("unchecked")
681 public void setValueAt(Object aValue, int row, int column) {
682 dataVector.elementAt(row).setElementAt(aValue, column);
683 fireTableCellUpdated(row, column);
684 }
685
686 //
687 // Protected Methods
688 //
689
690 /**
691 * Returns a vector that contains the same objects as the array.
692 * @param anArray the array to be converted
693 * @return the new vector; if <code>anArray</code> is <code>null</code>,
694 * returns <code>null</code>
695 */
696 protected static Vector<Object> convertToVector(Object[] anArray) {
697 if (anArray == null) {
698 return null;
699 }
700 Vector<Object> v = new Vector<>(anArray.length);
701 for (Object o : anArray) {
702 v.addElement(o);
703 }
704 return v;
705 }
706
707 /**
708 * Returns a vector of vectors that contains the same objects as the array.
709 * @param anArray the double array to be converted
710 * @return the new vector of vectors; if <code>anArray</code> is
711 * <code>null</code>, returns <code>null</code>
712 */
713 @SuppressWarnings("rawtypes")
714 protected static Vector<Vector<Object>> convertToVector(Object[][] anArray) {
715 if (anArray == null) {
716 return null;
717 }
718 @SuppressWarnings("rawtypes")
719 Vector<Vector> v = new Vector<>(anArray.length);
720 for (Object[] o : anArray) {
721 v.addElement(convertToVector(o));
722 }
723 return v;
724 }
725
726 } // End of class DefaultTableModel
|