我有一个JTable
id member_id amount
20 1 120
19 1 5400
18 2 580
17 3 620
16 2 250
当右键单击一行并单击一个弹出菜单项时,它会正确删除JTable中要删除的行,但是当我在用于填充JTable的对象列表中找到已删除的记录时,它总是选择下一个记录而不是。
例如,如果选择了id = 20的行,如果我通过使用getSelectedRow在我使用的对象列表中找到相应的记录来使用JTable的索引,我得到匹配id = 19的对象,等等上。当选择id = 16时," AWT-EventQueue-0" java.lang.IndexOutOfBoundsException"在控制台中显示
我使用AbstractTableModel实现来操作JTable和List来获取和填充表中的所有数据
我的代码
private List<AdvancePayment> payments = memberHandler.getAdvancePaidMembers();
这是单击删除menuitem时使用的内容
private void deleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
int row = tableAdvancePayment.getSelectedRow();
model.deleteRow(row);
int indexModel = tableAdvancePayment.convertRowIndexToModel(row);
AdvancePayment deletedPayment = payments.get(indexModel);
//This gets wrong record. Always return next object from the payments List
}
负责的敌人删除列表中的记录
public void deleteRow(int index) {
advanceList.remove(index);
System.out.println("Row index: "+index);
fireTableRowsDeleted(index, index);
}
我不明白为什么删除的索引与用于填充数据库的同一列表中的正确记录不匹配。请协助。如果您需要更多代码,请不要犹豫。
我的AbstractTableModel实现
package TableModels;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import models.AdvancePayment;
/**
*
* @author Administrator
*/
public class AdvancePaymentTableModel extends AbstractTableModel{
private List<AdvancePayment> advanceList;
private String[] columnNames = {"MNO", "NAME", "MONTH", "ADVANCE AMOUNT"};
private final Class[] columnClass = new Class[] {
Integer.class, String.class, String.class, Float.class
};
public AdvancePaymentTableModel(List<AdvancePayment> advanceList) {
this.advanceList = advanceList;
}
@Override
public String getColumnName(int column)
{
return columnNames[column];
}
@Override
public Class<?> getColumnClass(int columnIndex)
{
return columnClass[columnIndex];
}
public void addRow(AdvancePayment rowData)
{
advanceList.add( 0, rowData );
fireTableRowsInserted(advanceList.size() - 1, advanceList.size() - 1);
}
public void deleteRow(int index) {
advanceList.remove(index);
System.out.println("Row index: "+index);
fireTableRowsDeleted(index, index);
}
@Override
public int getRowCount() {
return advanceList.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
AdvancePayment row = advanceList.get(rowIndex);
if(0 == columnIndex) {
return row.getMNO();
}
else if(1 == columnIndex) {
return row.getName();
}
else if(2 == columnIndex) {
return getMonthString(row.getAdvanceMonth());
}
else if(3 == columnIndex) {
return row.getAmountPaid();
}
return null;
}
}
这是我用来制作带有
的列表的对象模型 public class AdvancePayment {
private int member_id, advance_year, advance_month, mno;
private long id;
private float amount_paid;
private String name;
public int getMNO() {
return mno;
}
public void setMNO(int mno) {
this.mno = mno;
}
public int getMemberId() {
return member_id;
}
public void setMemberId(int member_id) {
this.member_id = member_id;
}
public int getAdvanceYear() {
return advance_year;
}
public void setAdvanceYear(int advance_year) {
this.advance_year = advance_year;
}
public int getAdvanceMonth() {
return advance_month;
}
public void setAdvanceMonth(int advance_month) {
this.advance_month = advance_month;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public float getAmountPaid() {
return amount_paid;
}
public void setAmountPaid(float amount_paid) {
this.amount_paid = amount_paid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:1)
首先获取要删除的付款,然后将其从表格中删除。
示例实现如下所示:
private void deleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
int row = tableAdvancePayment.getSelectedRow();
AdvancePayment deletedPayment = payments.get(indexModel);
model.deleteRow(row);
//do something with deletedPayment
}