我有一个链接到 MySQL 数据库的 jXTable。我正在使用 DefaultTableModel。到目前为止,我已经有了它,用户可以编辑表中的数据。我一直在尝试添加“插入新行”按钮,但没有成功。同时,我认为每次用户更新表格时,我都可以在表格末尾添加一个空行。我能够使用 addRow() 方法获得我的空行,但它不可编辑。到目前为止,这是我的代码。有关如何使空行可编辑的任何提示?
public void rowSetChanged(RowSetEvent event) {
Object[] record;
Object[] emptyrow;
String [] columnNames=new String [] {"DATE RECEIVED", "INCOME NAME",
"DESCRIPTION", "AMOUNT", "FUND ALLOCATION"};
DefaultTableModel dtm=new DefaultTableModel(columnNames,0){
Class[] types = new Class [] {
java.sql.Date.class, java.lang.String.class, java.lang.String.class,
java.lang.Object.class, java.lang.Float.class, java.lang.String.class};
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
};
try {
IncomeUI.frs.beforeFirst();
while (IncomeUI.frs.next()){
record=new Object[]{IncomeUI.frs.getDate("DateReceived"),
IncomeUI.frs.getString("IncomeName"), IncomeUI.frs.getString("Description"),
IncomeUI.frs.getBigDecimal("Amount"),IncomeUI.frs.getString("FundAllocation")};
dtm.addRow(record);
}
} catch (SQLException ex) {ex.printStackTrace();}
emptyrow = new Object[]{" ", " ", " ", " ", " "};
dtm.addRow(emptyrow);
this.setModel(dtm);
this.setShowHorizontalLines(false);
this.setShowVerticalLines(false);
IncomeUI.jScrollPane2.setViewportView(this);
//System.out.println(event.toString());
}