Java 如何从 JTable 中删除一行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1117888/
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-08-11 23:58:02  来源:igfitidea点击:

How to remove a row from JTable?

javaswingjtable

提问by

I want to remove some rows from a JTable. How can I do it?

我想从 JTable 中删除一些行。我该怎么做?

回答by dave

A JTable normally forms the View part of an MVC implementation. You'll want to remove rows from your model. The JTable, which should be listening for these changes, will update to reflect this removal. Hence you won't find removeRow() or similar as a method on JTable.

JTable 通常构成 MVC 实现的 View 部分。您需要从模型中删除行。应该监听这些更改的 JTable 将更新以反映此删除。因此,您不会在 JTable 上找到 removeRow() 或类似方法。

回答by Jon

Look at the DefaultTableModel for a simple model that you can use:

查看 DefaultTableModel 以获取您可以使用的简单模型:

http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html

http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html

This extends the AbstractTableModel, but should be sufficient for basic purposes. You can always extend AbstractTableModel and create your own. Make sure you set it on the JTable as well.

这扩展了 AbstractTableModel,但应该足以满足基本目的。您始终可以扩展 AbstractTableModel 并创建自己的。确保您也在 JTable 上设置它。

http://java.sun.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html

http://java.sun.com/javase/6/docs/api/javax/swing/table/AbstractTableModel.html

Look at the basic Sun tutorial for more information on using the JTable with the table model:

查看基本的 Sun 教程,了解有关将 JTable 与表模型一起使用的更多信息:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data

回答by Brandon E Taylor

In order to remove a row from a JTable, you need to remove the target row from the underlying TableModel. If, for instance, your TableModel is an instance of DefaultTableModel, you can remove a row by doing the following:

为了从 JTable 中删除一行,您需要从底层TableModel 中删除目标行。例如,如果您的 TableModel 是DefaultTableModel 的一个实例,您可以通过执行以下操作来删除一行:

((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);

回答by codethulhu

The correct way to apply a filter to a JTable is through the RowFilterinterface added to a TableRowSorter. Using this interface, the view of a model can be changed without changing the underlying model. This strategy preserves the Model-View-Controller paradigm, whereas removing the rows you wish hidden from the model itself breaks the paradigm by confusing your separation of concerns.

将过滤器应用于 JTable 的正确方法是通过添加到 TableRowSorter的RowFilter接口。使用此接口,可以在不更改底层模型的情况下更改模型的视图。此策略保留了模型-视图-控制器范式,而删除您希望从模型本身中隐藏的行会混淆您的关注点分离,从而破坏了范式。

回答by Kirill Strizhak

If you need a simple working solution, try using DefaultTableModel.

如果您需要一个简单的工作解决方案,请尝试使用DefaultTableModel

If you have created your own table model, that extends AbstractTableModel, then you should also implement removeRow() method. The exact implementation depends on the underlying structure, that you have used to store data.

如果您创建了自己的表模型,它扩展了 AbstractTableModel,那么您还应该实现 removeRow() 方法。确切的实现取决于您用来存储数据的底层结构。

For example, if you have used Vector, then it may be something like this:

例如,如果您使用过 Vector,那么它可能是这样的:

public class SimpleTableModel extends AbstractTableModel {
    private Vector<String> columnNames = new Vector<String>();
    // Each value in the vector is a row; String[] - row data;
    private Vector<String[]> data = new Vector<String[]>();

    ...

    public String getValueAt(int row, int col) {
        return data.get(row)[col];
    }

    ...

    public void removeRow(int row) {
        data.removeElementAt(row);
    }
}

If you have used List, then it would be very much alike:

如果您使用过 List,那么它会非常相似:

// Each item in the list is a row; String[] - row data;
List<String[]> arr = new ArrayList<String[]>();

public void removeRow(int row) {
    data.remove(row);
}

HashMap:

哈希映射:

//Integer - row number; String[] - row data;
HashMap<Integer, String[]> data = new HashMap<Integer, String[]>();

public void removeRow(Integer row) {
    data.remove(row);
}

And if you are using arrays like this one

如果你使用这样的数组

String[][] data = { { "a", "b" }, { "c", "d" } };

then you're out of luck, because there is no way to dynamically remove elements from arrays. You may try to use arrays by storing separately some flags notifying which rows are deleted and which are not, or by some other devious way, but I would advise against it... That would introduce unnecessary complexity, and would in fact just be solving a problem by creating another. That's a sure-fire way to end up here. Try one of the above ways to store your table data instead.

那么你就不走运了,因为没有办法从数组中动态删除元素。您可以尝试通过单独存储一些标志来使用数组,通知哪些行被删除,哪些不被删除,或者通过其他一些狡猾的方式,但我建议不要这样做......这会引入不必要的复杂性,实际上只是解决通过创建另一个问题。这是一个确定的方式来结束这里。尝试使用上述方法之一来存储表数据。

For better understanding of how this works, and what to do to make your own model work properly, I stronglyadvise you to refer to Java Tutorial, DefaultTableModel APIand it's source code.

为了更好地理解这是如何工作的,以及如何使您自己的模型正常工作,我强烈建议您参考Java 教程DefaultTableModel API及其源代码

回答by cmujica

mmm is very simple guys

嗯是很简单的家伙

for( int i = model.getRowCount() - 1; i >= 0; i-- )
{
    model.removeRow(i);
}