如何使用自定义 TableModel 删除 JTable 中的行
我一直在阅读与我类似的帖子,并通读 Java教程页面,但我似乎无法让它工作。我不确定我是否遗漏了一些基本的东西...
我下面有一个自定义表模型,我需要能够从中删除行。该表初始化为空,并通过组合框和添加按钮添加行。还有一个删除按钮,需要将选定的行从表中删除。
class TableModel extends AbstractTableModel
{
private String[] columnNames = {"Enabled", "Value" };
protected Class[] columnClasses = new Class[] { Boolean.class, String.class };
public int getColumnCount() { return columnNames.length; }
public int getRowCount() { return filters.size(); }
public String getColumnName(int col) { return columnNames[col]; }
public Class getColumnClass(int col) { return columnClasses[col]; }
public Object getValueAt(int row, int col) { ... }
public void setValueAt(Object value, int row, int col) { ... }
public void addRow(String value)
{
fireTableRowsInserted(filters.size() - 1, filters.size() - 1);
int row = filters.size() -1 ;
int col = 1;
setValueAt(value, row, col);
}
public void removeRow(int row)
{
fireTableRowsDeleted(selectedRow, selectedRow);
}
}
我已通过控制台中的打印确认 selectedRow
包含正确的行。 fireTableRowsDeleted
函数只是不执行任何操作。该行仍然存在。如何删除特定行?
谢谢,
I've been reading posts similar to mine, and reading through the Java tutorial page but I just can't seem to get this working. I'm not sure if I'm missing something fundamental or not...
I have a custom table model below that I need to be able to delete rows from. The table is initialized empty and rows are added through a combo box and an add button. There is also a delete button that needs to delete the selected row out of the table.
class TableModel extends AbstractTableModel
{
private String[] columnNames = {"Enabled", "Value" };
protected Class[] columnClasses = new Class[] { Boolean.class, String.class };
public int getColumnCount() { return columnNames.length; }
public int getRowCount() { return filters.size(); }
public String getColumnName(int col) { return columnNames[col]; }
public Class getColumnClass(int col) { return columnClasses[col]; }
public Object getValueAt(int row, int col) { ... }
public void setValueAt(Object value, int row, int col) { ... }
public void addRow(String value)
{
fireTableRowsInserted(filters.size() - 1, filters.size() - 1);
int row = filters.size() -1 ;
int col = 1;
setValueAt(value, row, col);
}
public void removeRow(int row)
{
fireTableRowsDeleted(selectedRow, selectedRow);
}
}
I have confirmed that selectedRow
contains the correct row through prints in the console. The fireTableRowsDeleted
function just doesn't do anything. The row still exists. How do you just delete a specific row?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对
fireTableRowsDeleted
的调用只是触发事件来指示行已被删除,您仍然需要实际将它们从模型中删除。The call to
fireTableRowsDeleted
simply fires off the event to indicate rows have been deleted, you still need to actually remove them from the model.我发布此消息后立即就明白了。
行内容基于过滤器列表:
我的问题是我试图删除一行而不将其从列表中删除。因此,我将
removeRow()
修改为以下内容:它的作用就像一个魅力。
干杯
Immediately after I posted this I figured it out.
The rows contents are based on a List of filters:
My problem was I was trying to delete a row without removing that from the list. So I modified
removeRow()
to be the following:And it works like a charm.
cheers
我认为这就是答案:
编辑完成后该行将被删除。
I think this is the answer:
The row will be deleted when the editing is finished.