public void setColumnIdentifiers(Object[] newIdentifiers) { setColumnIdentifiers(convertToVector(newIdentifiers)); } /** * Sets the number of columns in the model. If the new size is greater * than the current size, new columns are added to the end of the model * with <code>null</code> cell values. * If the new size is less than the current size, all columns at index * <code>columnCount</code> and greater are discarded. * * @param columnCount the new number of columns in the model * * @see #setColumnCount * @since 1.3 */ public void setColumnCount(int columnCount) { columnIdentifiers.setSize(columnCount); justifyRows(0, getRowCount()); fireTableStructureChanged(); } /** * Adds a column to the model. The new column will have the * identifier <code>columnName</code>, which may be null. This method * will send a * <code>tableChanged</code> notification message to all the listeners. * This method is a cover for <code>addColumn(Object, Vector)</code> which * uses <code>null</code> as the data vector. * * @param columnName the identifier of the column being added */ public void addColumn(Object columnName) { addColumn(columnName, (Vector)null); } /** * Adds a column to the model. The new column will have the * identifier <code>columnName</code>, which may be null. * <code>columnData</code> is the * optional vector of data for the column. If it is <code>null</code> * the column is filled with <code>null</code> values. Otherwise, * the new data will be added to model starting with the first * element going to row 0, etc. This method will send a * <code>tableChanged</code> notification message to all the listeners. * * @param columnName the identifier of the column being added * @param columnData optional data of the column being added */ public void addColumn(Object columnName, Vector columnData) { columnIdentifiers.addElement(columnName); if (columnData != null) { int columnSize = columnData.size(); if (columnSize > getRowCount()) { dataVector.setSize(columnSize); } justifyRows(0, getRowCount()); int newColumn = getColumnCount() - 1; for(int i = 0; i < columnSize; i++) { Vector row = (Vector)dataVector.elementAt(i); row.setElementAt(columnData.elementAt(i), newColumn); } } else { justifyRows(0, getRowCount()); } fireTableStructureChanged(); } /** * Adds a column to the model. The new column will have the * identifier <code>columnName</code>. <code>columnData</code> is the * optional array of data for the column. If it is <code>null</code> * the column is filled with <code>null</code> values. Otherwise, * the new data will be added to model starting with the first * element going to row 0, etc. This method will send a * <code>tableChanged</code> notification message to all the listeners. * * @see #addColumn(Object, Vector) */ public void addColumn(Object columnName, Object[] columnData) { addColumn(columnName, convertToVector(columnData)); } //// Implementing the TableModel interface// /** * Returns the number of rows in this data table. * @return the number of rows in the model */ public int getRowCount() { return dataVector.size(); } /** * Returns the number of columns in this data table. * @return the number of columns in the model */ public int getColumnCount() { return columnIdentifiers.size(); } /** * Returns the column name. * * @return a name for this column using the string value of the * appropriate member in <code>columnIdentifiers</code>. * If <code>columnIdentifiers</code> does not have an entry * for this index, returns the default * name provided by the superclass. */ public String getColumnName(int column) { Object id = null; // This test is to cover the case when // getColumnCount has been subclassed by mistake ... if (column < columnIdentifiers.size() && (column >= 0)) { id = columnIdentifiers.elementAt(column); } return (id == null) ? super.getColumnName(column) : id.toString(); } /** * Returns true regardless of parameter values. * * @param row the row whose value is to be queried * @param column the column whose value is to be queried * @return true * @see #setValueAt */ public boolean isCellEditable(int row, int column) { return true; } /** * Returns an attribute value for the cell at <code>row</code> * and <code>column</code>. * * @param row the row whose value is to be queried * @param column the column whose value is to be queried * @return the value Object at the specified cell * @exception ArrayIndexOutOfBoundsException if an invalid row or * column was given */ public Object getValueAt(int row, int column) { Vector rowVector = (Vector)dataVector.elementAt(row); return rowVector.elementAt(column); } /** * Sets the object value for the cell at <code>column</code> and * <code>row</code>. <code>aValue</code> is the new value. This method * will generate a <code>tableChanged</code> notification. * * @param aValue the new value; this can be null * @param row the row whose value is to be changed * @param column the column whose value is to be changed * @exception ArrayIndexOutOfBoundsException if an invalid row or * column was given */ public void setValueAt(Object aValue, int row, int column) { Vector rowVector = (Vector)dataVector.elementAt(row); rowVector.setElementAt(aValue, column); fireTableCellUpdated(row, column); } //// Protected Methods// /** * Returns a vector that contains the same objects as the array. * @param anArray the array to be converted * @return the new vector; if <code>anArray</code> is <code>null</code>, * returns <code>null</code> */ protected static Vector convertToVector(Object[] anArray) { if (anArray == null) { return null; } Vector<Object> v = new Vector<Object>(anArray.length); for (Object o : anArray) { v.addElement(o); } return v; } /** * Returns a vector of vectors that contains the same objects as the array. * @param anArray the double array to be converted * @return the new vector of vectors; if <code>anArray</code> is * <code>null</code>, returns <code>null</code> */ protected static Vector convertToVector(Object[][] anArray) { if (anArray == null) { return null; } Vector<Vector> v = new Vector<Vector>(anArray.length); for (Object[] o : anArray) { v.addElement(convertToVector(o)); } return v; } } // End of class DefaultTableModelLines 501–708 of 708. Previous lines
public void setColumnIdentifiers(Object[] newIdentifiers) { setColumnIdentifiers(convertToVector(newIdentifiers)); } /** * Sets the number of columns in the model. If the new size is greater * than the current size, new columns are added to the end of the model * with <code>null</code> cell values. * If the new size is less than the current size, all columns at index * <code>columnCount</code> and greater are discarded. * * @param columnCount the new number of columns in the model * * @see #setColumnCount * @since 1.3 */ public void setColumnCount(int columnCount) { columnIdentifiers.setSize(columnCount); justifyRows(0, getRowCount()); fireTableStructureChanged(); } /** * Adds a column to the model. The new column will have the * identifier <code>columnName</code>, which may be null. This method * will send a * <code>tableChanged</code> notification message to all the listeners. * This method is a cover for <code>addColumn(Object, Vector)</code> which * uses <code>null</code> as the data vector. * * @param columnName the identifier of the column being added */ public void addColumn(Object columnName) { addColumn(columnName, (Vector)null); } /** * Adds a column to the model. The new column will have the * identifier <code>columnName</code>, which may be null. * <code>columnData</code> is the * optional vector of data for the column. If it is <code>null</code> * the column is filled with <code>null</code> values. Otherwise, * the new data will be added to model starting with the first * element going to row 0, etc. This method will send a * <code>tableChanged</code> notification message to all the listeners. * * @param columnName the identifier of the column being added * @param columnData optional data of the column being added */ public void addColumn(Object columnName, Vector columnData) { columnIdentifiers.addElement(columnName); if (columnData != null) { int columnSize = columnData.size(); if (columnSize > getRowCount()) { dataVector.setSize(columnSize); } justifyRows(0, getRowCount()); int newColumn = getColumnCount() - 1; for(int i = 0; i < columnSize; i++) { Vector row = (Vector)dataVector.elementAt(i); row.setElementAt(columnData.elementAt(i), newColumn); } } else { justifyRows(0, getRowCount()); } fireTableStructureChanged(); } /** * Adds a column to the model. The new column will have the * identifier <code>columnName</code>. <code>columnData</code> is the * optional array of data for the column. If it is <code>null</code> * the column is filled with <code>null</code> values. Otherwise, * the new data will be added to model starting with the first * element going to row 0, etc. This method will send a * <code>tableChanged</code> notification message to all the listeners. * * @see #addColumn(Object, Vector) */ public void addColumn(Object columnName, Object[] columnData) { addColumn(columnName, convertToVector(columnData)); } // // Implementing the TableModel interface // /** * Returns the number of rows in this data table. * @return the number of rows in the model */ public int getRowCount() { return dataVector.size(); } /** * Returns the number of columns in this data table. * @return the number of columns in the model */ public int getColumnCount() { return columnIdentifiers.size(); } /** * Returns the column name. * * @return a name for this column using the string value of the * appropriate member in <code>columnIdentifiers</code>. * If <code>columnIdentifiers</code> does not have an entry * for this index, returns the default * name provided by the superclass. */ public String getColumnName(int column) { Object id = null; // This test is to cover the case when // getColumnCount has been subclassed by mistake ... if (column < columnIdentifiers.size() && (column >= 0)) { id = columnIdentifiers.elementAt(column); } return (id == null) ? super.getColumnName(column) : id.toString(); } /** * Returns true regardless of parameter values. * * @param row the row whose value is to be queried * @param column the column whose value is to be queried * @return true * @see #setValueAt */ public boolean isCellEditable(int row, int column) { return true; } /** * Returns an attribute value for the cell at <code>row</code> * and <code>column</code>. * * @param row the row whose value is to be queried * @param column the column whose value is to be queried * @return the value Object at the specified cell * @exception ArrayIndexOutOfBoundsException if an invalid row or * column was given */ public Object getValueAt(int row, int column) { Vector rowVector = (Vector)dataVector.elementAt(row); return rowVector.elementAt(column); } /** * Sets the object value for the cell at <code>column</code> and * <code>row</code>. <code>aValue</code> is the new value. This method * will generate a <code>tableChanged</code> notification. * * @param aValue the new value; this can be null * @param row the row whose value is to be changed * @param column the column whose value is to be changed * @exception ArrayIndexOutOfBoundsException if an invalid row or * column was given */ public void setValueAt(Object aValue, int row, int column) { Vector rowVector = (Vector)dataVector.elementAt(row); rowVector.setElementAt(aValue, column); fireTableCellUpdated(row, column); } // // Protected Methods // /** * Returns a vector that contains the same objects as the array. * @param anArray the array to be converted * @return the new vector; if <code>anArray</code> is <code>null</code>, * returns <code>null</code> */ protected static Vector convertToVector(Object[] anArray) { if (anArray == null) { return null; } Vector<Object> v = new Vector<Object>(anArray.length); for (Object o : anArray) { v.addElement(o); } return v; } /** * Returns a vector of vectors that contains the same objects as the array. * @param anArray the double array to be converted * @return the new vector of vectors; if <code>anArray</code> is * <code>null</code>, returns <code>null</code> */ protected static Vector convertToVector(Object[][] anArray) { if (anArray == null) { return null; } Vector<Vector> v = new Vector<Vector>(anArray.length); for (Object[] o : anArray) { v.addElement(convertToVector(o)); } return v; } } // End of class DefaultTableModel