您好stackoverflow社区。我在执行UPDATE语句后刷新JTable时遇到问题。我在自己的TableModel类中创建了一个tableUpdate方法,但它似乎不起作用。我在下面的班级的ActionListener中调用它。如果有人能告诉我我做错了什么我会很感激。
class MyTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Member> members = new ArrayList<Member>();
public MyTableModel(List<Member> members) {
this.members = members;
}
public void addMember(Member m) {
members.add(m);
int row = members.size() - 1;
fireTableRowsInserted(row, row);
}
public void removeMemberFromTable(int row) {
members.remove(row);
fireTableRowsDeleted(row, row);
}
public void tableUpdate(int row, Member m) {
members.set(row, m);
fireTableDataChanged();
}
public List<Member> getMembers() {
return new ArrayList<Member>(members);
}
@Override
public int getColumnCount() {
return 8;
}
@Override
public int getRowCount() {
return members.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Member m = members.get(rowIndex);
switch (columnIndex) {
case 0:
return m.getMemberId();
case 1:
return m.getfName();
case 2:
return m.getlName();
case 3:
return m.getAddress();
case 4:
return m.getZipCode();
case 5:
return m.getCity();
case 6:
return m.getEmail();
case 7:
return m.getPhoneNo();
default:
return "";
}
}
public String getColumnName(int column) {
switch (column) {
case 0:
return "MedlemsID";
case 1:
return "Fornavn";
case 2:
return "Efternavn";
case 3:
return "Adresse";
case 4:
return "Postnummer";
case 5:
return "By";
case 6:
return "Email";
case 7:
return "Telefonnr";
default:
return "";
}
}
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 1:
return Integer.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return String.class;
}
}
}
editBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int zipCode = Integer.parseInt(zipCodeTF.getText());
Member m = new Member(fNameTF.getText(), lNameTF.getText(),
addressTF.getText(), zipCode, cityTF.getText(),
emailTF.getText(), phoneNoTF.getText());
MemberMapper mMapper = new MemberMapper();
int i = gui.getIndex();
mMapper.editMember(m, i);
editMemberFrame.setVisible(false);
List<Member> data = mMapper.selectAllMembers();
MyTableModel myTableModel = new MyTableModel(data);
int row = gui.getTable().getSelectedRow();
myTableModel.tableUpdate(row, m);
}
});
答案 0 :(得分:0)
看一下这个片段......
MyTableModel myTableModel = new MyTableModel(data);
int row = gui.getTable().getSelectedRow();
myTableModel.tableUpdate(row, m);
您正在创建一个新的表模型,但它与表格无关...
我还会使用fireTableRowsUpdates
代替fireTableDateChanged
,因为它不是戏剧性的更新