Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 272
Simeon Avatar asked Aug 20 '13 14:08

Simeon


People also ask

What is JTable used for?

The JTable is used to display and edit regular two-dimensional tables of cells. See How to Use Tables in The Java Tutorial for task-oriented documentation and examples of using JTable .

How do I know if a JTable row is selected?

JTable has a getSelectionModel() method which will give you a ListSelectionModel object. It tells you what rows are selected. You can add a ListSelectionListener to that via the addL..S..

How can you change the appearance of data in cells in JTable?

We can change the background and foreground color for each column of a JTable by customizing the DefaultTableCellRenderer class and it has only one method getTableCellRendererComponent() to implement it.


2 Answers

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

like image 81
NiziL Avatar answered Nov 18 '22 17:11

NiziL


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.

like image 36
camickr Avatar answered Nov 18 '22 15:11

camickr