7

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.

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();

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);

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.

Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
Simeon
  • 748
  • 1
  • 9
  • 26

2 Answers2

8

Here is the implementation of JTable#clearSelection()

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

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

From Oracle tutorial :

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

NiziL
  • 5,068
  • 23
  • 33
  • Thank you for your kind explanations. So If I got you right, selectionModel.clearSelection() clears the selection of rows and columnModel.getSelectionModel().clearSelection() clears the selection of columns, so that both together will clear a selection on a cell, correct? Since your explenation I got the feeling that calling mytable.clearSelection() is to be prefered to mytable.getSelectionModel().clearSelection(), since it does a more extensive clearing. Thank you for the hint about the ListSelectionModel. – Simeon Aug 20 '13 at 15:16
  • @Simeon It should be something like that, except I think only clear the selection of columns (or row) is enough to deselect a cell. (if there is no row selected, how a cell can be selected. Same reasoning with column). Glad to be helpful ;) – NiziL Aug 20 '13 at 16:21
5

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.

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.

camickr
  • 321,443
  • 19
  • 166
  • 288