如何在 Jtable 中添加包含序列号 (S.No.) 的行,每次删除表的数据时该行都会更新? 例如。: 我有一个 jtable,其中包含数据库中的数据
Name Age Class
ram 14 9
hari 15 9
rama 15 10
我希望它是这样的:
S.No. Name Age Class
1 Ram 14 9
2 hari 15 9
3 rama 15 10
如果我删除 hari 的数据,该表应如下所示:
S.No. Name Age Class
1 Ram 14 9
2 rama 15 10
最佳答案
此代码假设您维护所有学生的列表,并且类(class)中的任何位置都可以访问该表和模型。
public void stuff() {
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
list.remove(table.getSelectedRow());
removeRowsFromTable();
}
});
}
void removeRowsFromTable() throws Exception {
for (int i = table.getRowCount() - 1; i >= 0; i--) {
model.removeRow(i);
}
fillTable();
}
void fillTable() {
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
Object[] newRow = new Object[] {i, s.getName(),s.getAge(), s.getClass()};
model.addRow(newRow);
}
}
关于java - 如何在 Jtable 中添加序列号(S.No.),每次删除表的数据时该序列号都会更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49294891/