如何从 JTable 中删除一行?

发布于 2024-07-26 23:52:04 字数 33 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

深爱不及久伴 2024-08-02 23:52:04

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

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

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:

((DefaultTableModel)myJTable.getModel()).removeRow(rowToRemove);
不回头走下去 2024-08-02 23:52:04

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

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

例如,如果您使用过 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);
    }
}

如果您使用过 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);
}

如果您使用像这样的数组,

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

那么您就不走运了,因为没有办法动态地从数组中删除元素。 您可以尝试通过单独存储一些标志来通知哪些行被删除,哪些行未被删除,或者通过其他一些不正当的方式来使用数组,但我建议不要这样做......这会引入不必要的复杂性,实际上只是 < em>通过创造另一个问题来解决。 这是到达此处的可靠方式。 请尝试使用上述方法之一来存储表数据。

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

If you need a simple working solution, try using 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.

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

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:

// 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 strongly advise you to refer to Java Tutorial, DefaultTableModel API and it's source code.

£冰雨忧蓝° 2024-08-02 23:52:04

mmm 很简单,伙计们

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

mmm is very simple guys

for( int i = model.getRowCount() - 1; i >= 0; i-- )
{
    model.removeRow(i);
}
肥爪爪 2024-08-02 23:52:04

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

The correct way to apply a filter to a JTable is through the RowFilter interface 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.

﹂绝世的画 2024-08-02 23:52:04

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

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.

冷…雨湿花 2024-08-02 23:52:04

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

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

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

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

有关使用 JTable 和表模型的更多信息,请参阅基本的 Sun 教程

: sun.com/docs/books/tutorial/uiswing/components/table.html#data" rel="nofollow noreferrer">http://java.sun.com/docs/books/tutorial/uiswing/components/table.html #数据

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

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.

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:

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

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