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

发布于 2024-10-09 12:11:15 字数 1361 浏览 0 评论 0原文

我一直在阅读与我类似的帖子,并通读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一向肩并 2024-10-16 12:11:15

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.

疯狂的代价 2024-10-16 12:11:15

我发布此消息后立即就明白了。

行内容基于过滤器列表:

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

我的问题是我试图删除一行而不将其从列表中删除。因此,我将 removeRow() 修改为以下内容:

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

它的作用就像一个魅力。

干杯

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:

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

And it works like a charm.

cheers

[旋木] 2024-10-16 12:11:15

我认为这就是答案:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文