使用AbstractTableModel从JTable删除行 [英] remove rows from JTable with AbstractTableModel
本文介绍了使用AbstractTableModel从JTable删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用按钮使用AbstractTableModel
从JTable
中删除选定的行.
I would like to remove selected row from JTable
with AbstractTableModel
using a button.
下面的代码可与DefaultTableModel一起使用:
The code below works with DefaultTableModel:
...
MyTableModel mtb;
...
private String[]....
private Object[][]...
...
JTable table = new JTable(mtb)
JButton delete;
...
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(delete))
{
if(table.getSelectedRow()<0)
{
JOptionPane.showMessageDialog(this,"Select row");
}
else
{
mtb.removeRow(table.getSelectedRow());
}
}
}
,但它不适用于AbstractTablemodel
.
I have a little mess in my code so here is java example from oracle page that can be used:
谢谢!
推荐答案
对于AbstractTableModel
,您必须根据模型的内部数据结构实现自己的removeRow()
,但是您可以研究DefaultTableModel
作为引发哪些事件的指南.例如,
For AbstractTableModel
, you have to implement your own removeRow()
based on your model's internal data structure(s), but you can study the source of DefaultTableModel
as a guide on which event(s) to fire. For example,
public void removeRow(int row) {
// remove a row from your internal data structure
fireTableRowsDeleted(row, row);
}
这篇关于使用AbstractTableModel从JTable删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文