我有一个链接到 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());
}
最佳答案
好吧,虽然不是 100% 修复,但它确实有效。我最终只是添加了代码来添加一个带有假数据的新行(例如“编辑我”)。用户可以重新编辑它。目前有效。
关于java - JXTable如何让空行可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23029991/