Java JTable.clearSelection() 与 Jtable.getSelectionModel.clearSelection() - 何时使用什么?

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

JTable.clearSelection() vs Jtable.getSelectionModel.clearSelection() - When to use what?

javaswingjtableselectionselectionmodel

提问by Simeon

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 模型对象中的所有选择。Java 提供了这个函数“clearSelection()”,据我所知,它可以满足我的需求。

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:

但我很困惑为什么可以在 JTable 对象以及 JTable 对象的选择模型上调用此函数:

 1) mytable.clearSelection();
 2) mytable.getSelectionModel().clearSelection();

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

两种方式都有效,但我不明白在什么情况下 SelectionModel 的 clearSelection() (如 2) )会有意义。据我了解 SelectionModels,它们用于决定 JTable 允许什么样的选择。我使用 SelectionModel 只允许选择恰好一行

//allow only one row to be selected
mytable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

Which way is to be preferred in what kind of situation? Is there a good reason not to use way 1?

在什么样的情况下首选哪种方式?是否有充分的理由不使用方式 1?

I would be glad if anyone has some beginner friendly explanation for that. Thx in advance.

如果有人对此有一些对初学者友好的解释,我会很高兴。提前谢谢。

采纳答案by NiziL

Here is the implementation of JTable#clearSelection()

这里是实现 JTable#clearSelection()

public void clearSelection() {
    selectionModel.clearSelection();
    columnModel.getSelectionModel().clearSelection();
}

As you can see, there is two ListSelectionModelwhich are cleared, because you can select column and/or row and/or cell.

如您所见,有两个ListSelectionModel被清除,因为您可以选择列和/或行和/或单元格。

From Oracle tutorial :

来自 Oracle 教程:

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.

JTable 使用一个非常简单的选择概念,作为行和列的交集进行管理。它不是为处理完全独立的单元格选择而设计的。

A ListSelectionModelhandle 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

一个ListSelectionModel手柄选择的所有方面,如被选中的行,我们怎么能选择一些行,等等......不仅种类选择的!Oracle JTable 教程
更多信息

回答by camickr

Usually when you see two methods like that it is because the table will invoke the SelectionModel.clearSelection() method for you. So the table method is a convenience method.

通常,当您看到两个这样的方法时,这是因为表会为您调用 SelectionModel.clearSelection() 方法。所以table方法是一种方便的方法。

In this case the actual code is:

在这种情况下,实际代码是:

public void clearSelection() 
{
    selectionModel.clearSelection();
    columnModel.getSelectionModel().clearSelection();
}

So both the row and column selection models are cleared.

所以行和列选择模型都被清除了。