62,614
社区成员
发帖
与我相关
我的任务
分享
public class NewJFrame extends javax.swing.JFrame {
public NewJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jTable1.setEnabled(false);//让表不可被编辑
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
}
import java.util.Vector;
import javax.swing.table.AbstractTableModel;
public class StandardTableModel extends AbstractTableModel {
/**
* each element of this vector represents a single row of the table. Internally, a row is represented
* by a Object[].
*/
protected Vector<Object[]> rows = new Vector<Object[]>();
/**
* array that holds information on the names for the table columns
*/
protected String[] colNames = null;
/**
* 设置表格列是否允许编辑
*/
protected boolean[] editables = null;
public StandardTableModel(String[] colNames, boolean[] editables) {
super();
this.colNames = colNames;
this.editables = editables;
}
public int getColumnCount() {
return colNames.length;
}
public int getRowCount() {
return rows.size();
}
public String getColumnName(int column) {
return this.colNames[column];
}
/**
* deletes all data from the table (also imforms the GUI)
*/
public void clearTable(){
int numRows = this.getRowCount();
this.rows.clear();
if (numRows >= 1)
this.fireTableRowsDeleted(0, numRows - 1);
}
/**
* @param columnName name of column to get the index for
* @return the index of the column with the given name
*/
public int findColumn(String columnName) {
for ( int i=0; i<this.colNames.length; i++ ){
if (this.colNames[i].toLowerCase().equals(columnName.toLowerCase())) return i;
}
return -1;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
(this.rows.get(rowIndex))[columnIndex] = aValue;
this.fireTableCellUpdated(rowIndex,columnIndex);
}
public Object getValueAt(int rowIndex, int columnIndex) {
return (this.rows.get(rowIndex))[columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return editables[columnIndex];
}
public Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
/**
* simple method to add a row to the table.
* (It better matches the given column nummer and column types!!)
*@param newRow
*/
public void addRow(Object[] newRow){
this.rows.add(newRow);
this.fireTableRowsInserted(this.getRowCount()-1,this.getRowCount()-1);
}
/**
*@return array containing the names of the columns
*/
public String[] getColNames() {
return colNames;
}
/**
* Setting new column names will flush the table, if the new array has not the same length as the old one!
*@param colNames array containing new column names
*/
protected void setColNames(String[] colNames) {
if (this.colNames.length != colNames.length)
this.clearTable();
this.colNames = colNames;
}
}