📄 gentablemodel.java
字号:
/*
* @author Rangan
* @version 2.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the Application : GenTableModel.java
* Creation/Modification History :
*
* Rangan 24-Dec-1998 Created
* Venky 04-Jan-2002 Modified
* Anupama 04-Apr-2002 Modified
*
*/
package oracle.otnsamples.vsm.client;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
* This class maintains the data required for a JTable handling. It can be used
* as the table model, by any application using JTables. It encapsulates the
* maintenance of the JTable data, and also provides a few additional member
* functions: 1) insertRow: takes a ArrayList containing a new row values,
* and creates a new row in the table. 2) deleteRow: deletes the row specified
* from the displayed rows. 3) getRow: returns a ArrayList containing the
* row data. 4) updateRow: Replaces the ArrayList present in the row number
* specified with the new ArrayList passed. 5) clearTable: Clears table data
* The data itself is maintained in a ArrayList, and hence the table data is
* maintained efficiently, as the ArrayList can grow and shrink as and when
* the table data changes. The constructor takes an array of columnNames,
* number of rows to be created initially. Also it takes an array of default
* value object(which may be heterogeneous. This helps in deciding the column
* type. If more functionality is required, like setting columns as
* non-editable and changing cell-renderers, this class can be extended.
*/
public class GenTableModel extends AbstractTableModel {
private ArrayList data; // Holds the table data
private String[] columnNames; // Holds the column names.
/**
* Constructor: Initializes the table structure, including number of columns
* and column headings. Also initializes table data with default values.
*
* @param columnscolumns[] array of column titles.
* @param defaultvdefaultv array of default value objects, for each column.
* @param rowsrows number of rows initially.
*/
public GenTableModel(String[] columns, Object[] defaultv, int rows) {
// Initialize number of columns and column headings
columnNames = new String[ columns.length ];
for(int i = 0; i < columns.length; i++) {
columnNames [ i ] = new String(columns [ i ]);
}
// Instantiate Data ArrayList, and fill it up with default values
data = new ArrayList();
for(int i = 0; i < rows; i++) {
ArrayList cols = new ArrayList();
for(int j = 0; j < columns.length; j++) {
cols.add(defaultv [ j ]);
}
data.add(cols);
}
}
/**
* Overrides AbstractTableModel method. Returns the number of columns in
* table
*
* @return <b>int</b> number of columns in the table.
*/
public int getColumnCount() {
return columnNames.length;
}
/**
* Overrides AbstractTableModel method. Returns the number of rows in table
*
* @return <b>int</b> number of rows in the table.
*/
public int getRowCount() {
return data.size();
}
/**
* Overrides AbstractTableModel method.
*
* @param colcolumn number column number
*
* @return <b>String </b> column name.
*/
public String getColumnName(int col) {
return columnNames [ col ];
}
/**
* Overrides AbstractTableModel method.
*
* @param rowrows row number
* @param colrows column number
*
* @return <b>Object</b> the value at the specified cell.
*/
public Object getValueAt(int row, int col) {
ArrayList colArrayList = (ArrayList) data.get(row);
return colArrayList.get(col);
}
/**
* Overrides AbstractTableModel method. Returns the class for the specified
* column.
*
* @param colcol column number
*
* @return <b> Class </b> the class for the specified column.
*/
public Class getColumnClass(int col) {
// If value at given cell is null, return default class-String
return getValueAt(0, col) == null ? String.class
: getValueAt(0, col).getClass();
}
/**
* Overrides AbstractTableModel method. Sets the value at the specified cell
* to object.
*
* @param objObject
* @param rowrow row number
* @param colcolumn column number
*/
public void setValueAt(Object obj, int row, int col) {
ArrayList colArrayList = (ArrayList) data.get(row);
colArrayList.set(col, obj);
}
/**
* Adds a new row to the table.
*
* @param newrowArrayList new row data
*/
public void insertRow(ArrayList newrow) {
data.add(newrow);
super.fireTableDataChanged();
}
/**
* Deletes the specified row from the table.
*
* @param rowrow row number
*/
public void deleteRow(int row) {
data.remove(row);
super.fireTableDataChanged();
}
/**
* Delete all the rows existing after the selected row from the JTable
*
* @param rowrow row number
*/
public void deleteAfterSelectedRow(int row) {
// Get the initial size of the table before the deletion has started.
int size = this.getRowCount();
// The number of items to be deleted is got by subtracting the
// selected row + 1 from size. This is done because as each row is deleted
// the rows next to it are moved up by one space and the number of rows
// in the JTable decreases. So the technique used here is always deleting
// the row next to the selected row from the table n times so that all the
// rows after the selected row are deleted.
int n = size - (row + 1);
for(int i = 1; i <= n; i++) {
data.remove(row + 1);
}
super.fireTableDataChanged();
}
/**
* Returns the values at the specified row as a ArrayList.
*
* @param rowrow row number
*
* @return DOCUMENT ME!
*/
public ArrayList getRow(int row) {
return (ArrayList) data.get(row);
}
/**
* Updates the specified row. It replaces the row ArrayList at the specified
* row with the new ArrayList.
*
* @param updatedRowArrayList row data
* @param rowrow row number
*/
public void updateRow(ArrayList updatedRow, int row) {
data.set(row, updatedRow);
super.fireTableDataChanged();
}
/**
* Clears the table data.
*/
public void clearTable() {
data = new ArrayList();
super.fireTableDataChanged();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -