📄 defaulttablemodel.java
字号:
int rowsToAdd = rowCount - existingRowCount; for (int i = 0; i < rowsToAdd; i++) { Vector tmp = new Vector(); tmp.setSize(columnIdentifiers.size()); dataVector.add(tmp); } fireTableRowsInserted(existingRowCount,rowCount-1); } } /** * Sets the number of columns in the table. Existing rows are truncated * or padded with <code>null</code> values to match the new column count. * A {@link TableModelEvent} is sent to all registered listeners. * * @param columnCount the column count. */ public void setColumnCount(int columnCount) { for (int i = 0; i < dataVector.size(); ++i) { ((Vector) dataVector.get(i)).setSize(columnCount); } if (columnIdentifiers != null) columnIdentifiers.setSize(columnCount); fireTableStructureChanged(); } /** * Adds a column with the specified name to the table. All cell values * for the column are initially set to <code>null</code>. * * @param columnName the column name (<code>null</code> permitted). */ public void addColumn(Object columnName) { addColumn(columnName, (Object[]) null); } /** * Adds a column with the specified name and data values to the table. * * @param columnName the column name (<code>null</code> permitted). * @param columnData the column data. */ public void addColumn(Object columnName, Vector columnData) { Object[] dataArray = null; if (columnData != null) { int rowCount = dataVector.size(); if (columnData.size() < rowCount) columnData.setSize(rowCount); dataArray = columnData.toArray(); } addColumn(columnName, dataArray); } /** * Adds a column with the specified name and data values to the table. * * @param columnName the column name (<code>null</code> permitted). * @param columnData the column data. */ public void addColumn(Object columnName, Object[] columnData) { if (columnData != null) { // check columnData array for cases where the number of items // doesn't match the number of rows in the existing table if (columnData.length > dataVector.size()) { int rowsToAdd = columnData.length - dataVector.size(); for (int i = 0; i < rowsToAdd; i++) { Vector tmp = new Vector(); tmp.setSize(columnIdentifiers.size()); dataVector.add(tmp); } } else if (columnData.length < dataVector.size()) { Object[] tmp = new Object[dataVector.size()]; System.arraycopy(columnData, 0, tmp, 0, columnData.length); columnData = tmp; } } for (int i = 0; i < dataVector.size(); ++i) { ((Vector) dataVector.get(i)).add(columnData == null ? null : columnData[i]); } columnIdentifiers.add(columnName); fireTableStructureChanged(); } /** * Adds a new row containing the specified data to the table and sends a * {@link TableModelEvent} to all registered listeners. * * @param rowData the row data (<code>null</code> permitted). */ public void addRow(Vector rowData) { int rowIndex = dataVector.size(); dataVector.add(rowData); newRowsAdded(new TableModelEvent( this, rowIndex, rowIndex, -1, TableModelEvent.INSERT) ); } /** * Adds a new row containing the specified data to the table and sends a * {@link TableModelEvent} to all registered listeners. * * @param rowData the row data (<code>null</code> permitted). */ public void addRow(Object[] rowData) { addRow(convertToVector(rowData)); } /** * Inserts a new row into the table. * * @param row the row index. * @param rowData the row data. */ public void insertRow(int row, Vector rowData) { dataVector.add(row, rowData); fireTableRowsInserted(row,row); } /** * Inserts a new row into the table. * * @param row the row index. * @param rowData the row data. */ public void insertRow(int row, Object[] rowData) { insertRow(row, convertToVector(rowData)); } /** * Moves the rows from <code>startIndex</code> to <code>endIndex</code> * (inclusive) to the specified row. * * @param startIndex the start row. * @param endIndex the end row. * @param toIndex the row to move to. */ public void moveRow(int startIndex, int endIndex, int toIndex) { Vector removed = new Vector(); for (int i = endIndex; i >= startIndex; i--) { removed.add(this.dataVector.remove(i)); } for (int i = 0; i <= endIndex - startIndex; i++) { dataVector.insertElementAt(removed.get(i), toIndex); } int firstRow = Math.min(startIndex, toIndex); int lastRow = Math.max(endIndex, toIndex + (endIndex - startIndex)); fireTableRowsUpdated(firstRow, lastRow); } /** * Removes a row from the table and sends a {@link TableModelEvent} to * all registered listeners. * * @param row the row index. */ public void removeRow(int row) { dataVector.remove(row); fireTableRowsDeleted(row,row); } /** * Returns the number of rows in the model. * * @return The row count. */ public int getRowCount() { return dataVector.size(); } /** * Returns the number of columns in the model. * * @return The column count. */ public int getColumnCount() { return (columnIdentifiers == null ? 0 : columnIdentifiers.size()); } /** * Returns the name of the specified column. * * @param column the column index. * * @return The column name. */ public String getColumnName(int column) { String result = ""; if (columnIdentifiers == null) result = super.getColumnName(column); else { if (column < getColumnCount()) { Object id = columnIdentifiers.get(column); if (id != null) result = id.toString(); else result = super.getColumnName(column); } else result = super.getColumnName(column); } return result; } /** * Returns <code>true</code> if the specified cell can be modified, and * <code>false</code> otherwise. For this implementation, the method * always returns <code>true</code>. * * @param row the row index. * @param column the column index. * * @return <code>true</code> in all cases. */ public boolean isCellEditable(int row, int column) { return true; } /** * Returns the value at the specified cell in the table. * * @param row the row index. * @param column the column index. * * @return The value (<code>Object</code>, possibly <code>null</code>) at * the specified cell in the table. */ public Object getValueAt(int row, int column) { return ((Vector) dataVector.get(row)).get(column); } /** * Sets the value for the specified cell in the table and sends a * {@link TableModelEvent} to all registered listeners. * * @param value the value (<code>Object</code>, <code>null</code> permitted). * @param row the row index. * @param column the column index. */ public void setValueAt(Object value, int row, int column) { ((Vector) dataVector.get(row)).set(column, value); fireTableCellUpdated(row,column); } /** * Converts the data array to a <code>Vector</code>. * * @param data the data array (<code>null</code> permitted). * * @return A vector (or <code>null</code> if the data array * is <code>null</code>). */ protected static Vector convertToVector(Object[] data) { if (data == null) return null; Vector vector = new Vector(data.length); for (int i = 0; i < data.length; i++) vector.add(data[i]); return vector; } /** * Converts the data array to a <code>Vector</code> of rows. * * @param data the data array (<code>null</code> permitted). * * @return A vector (or <code>null</code> if the data array * is <code>null</code>. */ protected static Vector convertToVector(Object[][] data) { if (data == null) return null; Vector vector = new Vector(data.length); for (int i = 0; i < data.length; i++) vector.add(convertToVector(data[i])); return vector; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -