如何从 JTable 中删除一行?
我想从 JTable 中删除一些行。 我该怎么做?
I want to remove some rows from a JTable. How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想从 JTable 中删除一些行。 我该怎么做?
I want to remove some rows from a JTable. How can I do it?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
为了从 JTable 中删除一行,您需要从底层 TableModel。 例如,如果您的 TableModel 是 的实例DefaultTableModel,您可以通过执行以下操作来删除行:
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 。
如果您创建了自己的表模型,该模型扩展了 AbstractTableModel,那么您还应该实现 removeRow() 方法。 确切的实现取决于您用于存储数据的底层结构。
例如,如果您使用过 Vector,那么它可能是这样的:
如果您使用过 List,那么它会非常相似:
HashMap:
如果您使用像这样的数组,
那么您就不走运了,因为没有办法动态地从数组中删除元素。 您可以尝试通过单独存储一些标志来通知哪些行被删除,哪些行未被删除,或者通过其他一些不正当的方式来使用数组,但我建议不要这样做......这会引入不必要的复杂性,实际上只是 < 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:
If you have used List, then it would be very much alike:
HashMap:
And if you are using arrays like this one
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.
mmm 很简单,伙计们
mmm is very simple guys
将过滤器应用于 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.
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.
查看 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