JBuilder6中如何使用JTable(100分)

  • 主题发起人 主题发起人 tangts
  • 开始时间 开始时间
T

tangts

Unregistered / Unconfirmed
GUEST, unregistred user!

在JBuilder6中用以下代码,生成表后,不知如何新增或删除一行?
Object[][] data = {
{new String("12345"), new String("67890")},
{new String("54321"), new String("09876")}
};
String[] columnNames = {"帐号","口令"};
JTable jTable1 = new JTable(data,columnNames);
 
不用defaultTableModel
例如:我自己定义的一个InfoTableModel extends AbstractTableModel
import javax.swing.table.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class InfoTableModel extends AbstractTableModel {
private Object[][] data ={ };
final String[] names = {"状态","保存文件名","URL","百分率","所需时间"};
// These methods always need to be implemented.
public int getColumnCount() { return names.length;
}
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return data[row][col];}
// The default implementations of these methods in
// AbstractTableModel would work, but we can refine them.
public String getColumnName(int column) {return names[column];}
public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
public boolean isCellEditable(int row, int col) {return (col==4);}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = aValue;
}
public void addRow(Object[] objects) {
//定义一个比现在Data数组多一行的数组,开辟内存空间来存放新的Record
Object[][] dataTemp = new Object[getRowCount() + 1][getColumnCount()];
//复制数组
System.arraycopy(data, 0, dataTemp, 0, getRowCount());
//添加最后那项记录
for (int i=0;
i< getColumnCount();i++) {
dataTemp [getRowCount()] = objects;
}
//赋值生成的新的数组
data = dataTemp ;
//产生事件通知Listener们
fireTableDataChanged() ;
//fireTableRowsInserted()也是可以的,系统开销更小
}
}
在你需要添加的地方,使用:
InfoTableModel aModel = (InfoTableModel)jInfoTable.getModel();
Object[] ssRows = {"run","d//",URLdlg.getURLstring(),"23%","0:23:32"};
aModel.addRow(ssRows);
jInfoTable.setModel(aModel);
jInfoTable.updateUI();
 
一般通过TableModel来操作数据插入、删除。。。。
JTable mMinorTb;
.
.
DefaultTableModel mTableModel = (DefaultTableModel)mMinorTb.getModel();
mTableModel.addRow(Vector rowData)
mTableModel.insertRow(int row, Vector rowData)
mTableModel.removeRow(int row)
具体可参考JDKdo
c
 
后退
顶部