How can I deselect one row in JTable? - java

I have JTable with ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
When I use CTRL+mouse click, rows are selected and all are right. But if I want to deselect only one selected row and press CTRL+mouse click to one selected row, all rows are deselected, not only one. Then if I press CTRL+mouse click to some row again, previouse rows are selected again, but without row, that I wanted to deselect.
I want to deselect only one row using CTRL+mouse click. How can I do that?
EDIT:
I had this in my code:
table.getColumnModel().setColumnSelectionAllowed(true);
I found that if I remove this line then all works fine. But can JTable work correctly although 'ColumnSelectionAllowed' is true?

Try method clearSelection on JTable .
it works by calling clearSelection on the ListSelectionModel

If you are looking for deselection one row from many, use the below code:
table.getSelectionModel().removeSelectionInterval(rowFrom, rowTo);
for deselecting one row:
table.getSelectionModel().removeSelectionInterval(row, row);
you can use multiple selection like this
table.getSelectionModel().removeSelectionInterval(1, 3); //1,2,3 starting from 0
table.getSelectionModel().removeSelectionInterval(5, 5); //5
to add it back, use:
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
you can use multiple selection like this
table.getSelectionModel().addSelectionInterval(1, 2); //1,2
table.getSelectionModel().addSelectionInterval(4, 4); //4
to set it to specific range, which will clear your old selection first,
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);
Which it is equal to:
table.clearSelection();
table.getSelectionModel().addSelectionInterval(rowFrom, rowTo);

Related

How to edit multiple cells in JavaFX TableView?

I have a JavaFX TableView with some columns and some rows. Each column has a custom TableCell to be edited, for example: some columns use text fields, some others use check boxes, choice boxes and so on.
What I want is to edit several cells (simultaneously) in the same column by doing the following:
Select some rows in the TableView.
Click on a cell using the right mouse button.
A context menu is shown with the option "Edit All"; choose it.
Somehow I edit one of those cells and every cell in the same column whose row is selected commits the same value.
What I want to know is whether it is possible for every cell to “commitEdit” the same value. I cannot find the way of doing it.
Thanks in advance.

SWT Table - Deselecting a Row after it has been selected

When I select a row in my table, there is no way to deselect that row. Unless there is two rows in the table. Then I can select the other row. Now the 2nd row is selected and the 1st row is deselected. I thought by selecting the selected row again, that would deselect the row. But that does not seem to be the case for me.
viewer = new AplotDataTableViewer(parent, SWT.BORDER|SWT.V_SCROLL|SWT.MULTI|SWT.FULL_SELECTION);
I really don't want to have to create a button to deselect current selections.
TableViewer.setSelection(StructuredSelection.EMPTY) was the way I corrected the issue.
Sambi Reddy provided me with the solution

Selecting the Multiple Rows randomly in JTable

Can I select the multiple rows randomly in JTable?
I tried with
table.setRowSelectionInterval(index 0, index 2);
It will select three rows 0,1 and 2, but I need to skip the selection of index 1.
How can I accomplish this?
thanks in advance
There is no way to set a discontinous selection with a single method call, you need at least two:
table.setRowSelectionInterval(0, 0);
table.addRowSelectionInterval(2, 2);
set Jtable properties-
selectionModel- Multiple Interval Selection
The first row selection should be done with setRowSelectionInterval(i,j)
The subsequent row selections (in the same JTable) should be done with addRowSelectionInterval(i,j)
Use
ListSelectionModel selectionModel =
jTable1.getSelectionModel();
selectionModel.setSelectionInterval(1, 1);//for first row selection
selectionModel.addSelectionInterval(3, 3);
selectionModel.addSelectionInterval(5, 5);
this
and link:
http://www.jguru.com/faq/view.jsp?EID=11760

How to select only cells in one row in a jtable?

I have a JTable consisting of multiple rows and columns. I want to make the cells selectable, but only in one row at a time. So for example, when I click on the cell in third row and the fifth column, I can pull the mouse to the left or right and select more cells, but only in this specific row and not in the row above or below.
How can I do this?
Use the setSelectionMode() method from ListSelectionModel interface, and set the selection mode to ListSelectionModel.SINGLE_SELECTION.
This will configure JTable to work with one row at a time selection, blocking selection of multiple rows.
To select single cells, combine the above with setColumnSelectionAllowed(true) on TableColumnModel, and you should get what you need.

Item always selected in JTable

I have created a JTable where the included items are provided with radio buttons in order to enable users to select any of these once at a time. In order to show always the first item selected when the JTable is initialised (or when the item previously selected has been deleted) what method should I use?
You should see the JTable Javadoc and particularly at :
getCellRect(int row, int column, boolean includeSpacing)
and
scrollRectToVisible(Rectangle aRect)
Something like
table.scrollRectToVisible(table.getCellRect(table.getSelectedRows()[0], 0, true));
should suit you.

Categories

Resources