java 如何使用自定义 TableModel 删除 JTable 中的一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4557913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:57:11  来源:igfitidea点击:

HowTo Remove a Row in a JTable with a Custom TableModel

javajtabledelete-rowtablemodelabstracttablemodel

提问by linsek

I've been reading posts similar to mine, and reading through the Java tutorial pagebut I just can't seem to get this working. I'm not sure if I'm missing something fundamental or not...

我一直在阅读与我类似的帖子,并通读Java 教程页面,但我似乎无法使其正常工作。我不确定我是否遗漏了一些基本的东西......

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 selectedRowcontains the correct row through prints in the console. The fireTableRowsDeletedfunction just doesn't do anything. The row still exists. How do you just delete a specific row?

我已经selectedRow通过控制台中的打印确认包含正确的行。该fireTableRowsDeleted功能只是不做任何事情。该行仍然存在。你如何只删除特定的行?

Thanks,

谢谢,

回答by vickirk

The call to fireTableRowsDeletedsimply fires off the event to indicate rows have been deleted, you still need to actually remove them from the model.

调用fireTableRowsDeleted只是触发事件以指示行已被删除,您仍然需要从模型中实际删除它们。

回答by linsek

Immediately after I posted this I figured it out.

在我发布此信息后,我立即想通了。

The rows contents are based on a List of filters:

行内容基于过滤器列表:

public int getRowCount() { return filters.size(); }

My problem was I was trying to delete a row without removing that from the list. So I modified removeRow()to be the following:

我的问题是我试图删除一行而不从列表中删除它。所以我修改removeRow()如下:

public void removeRow(int row)
{
    filters.remove(row);
    fireTableRowsDeleted(row, row);
}

And it works like a charm.

它就像一个魅力。

cheers

干杯

回答by user1138042

I think this is the answer:

我想这就是答案:

final int row = selectedRow;
EventQueue.invokeLater(new Runnable() {
       public void run() {
             model.removeRow(row);
       }
});

The row will be deleted when the editing is finished.

编辑完成后,该行将被删除。