JTable.clearSelection()vs Jtable.getSelectionModel.clearSelection()-什么时候使用什么? [英] JTable.clearSelection() vs Jtable.getSelectionModel.clearSelection() - When to use what?
问题描述
我需要取消JTable模型对象内的所有选择. Java提供了此功能"clearSelection()",据我所知,该功能可以满足我的需要.
I need to cancel all selections within a JTable model object. Java provides this function "clearSelection()" which does, what I need, as far as I understand.
但是我很困惑为什么可以在JTable对象以及JTable对象的选择模型上调用此函数:
But I am confused why this function can be called on a JTable object as well as on a selection model for a JTable object:
1) mytable.clearSelection();
2) mytable.getSelectionModel().clearSelection();
两种方法都有效,但是我不了解在什么情况下SelectionModel的clearSelection()(如2)有意义.据我了解SelectionModels,它们用于决定JTable允许的选择类型.我使用SelectionModel只允许选择精确的一行
Both ways work, but I do not understand in what situation a clearSelection() of a SelectionModel (like at 2) ) would make any sense. As far as I understood SelectionModels, they are used to decide what kind of selections a JTable allows. I use the SelectionModel to only allow a Selection of exactly one row
//allow only one row to be selected
mytable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
在哪种情况下首选哪种方式?是否有充分的理由不使用方法1?
Which way is to be preferred in what kind of situation? Is there a good reason not to use way 1?
如果有人对此有一些初学者友好的解释,我将感到高兴.提前谢谢.
I would be glad if anyone has some beginner friendly explanation for that. Thx in advance.
推荐答案
这是JTable#clearSelection()
public void clearSelection() {
selectionModel.clearSelection();
columnModel.getSelectionModel().clearSelection();
}
如您所见,有两个ListSelectionModel
已清除,因为您可以选择列和/或行和/或单元格.
As you can see, there is two ListSelectionModel
which are cleared, because you can select column and/or row and/or cell.
从Oracle教程开始:
From Oracle tutorial :
JTable使用一个非常简单的选择概念,作为一个 行和列的交集.它并非旨在完全处理 独立的单元格选择.
JTable uses a very simple concept of selection, managed as an intersection of rows and columns. It was not designed to handle fully independent cell selections.
A ListSelectionModel
处理选择的所有方面,例如选择了哪一行,如何选择某些行等...不仅是选择的种类!
有关更多信息,请参见 Oracle JTable教程
A ListSelectionModel
handle all aspect of the selection such as which row is selected, how can we select some rows, etc... Not only the kind of selection !
More information in the Oracle JTable tutorial
这篇关于JTable.clearSelection()vs Jtable.getSelectionModel.clearSelection()-什么时候使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!