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
Related
After looking through the JTable API I didn't see anything about this but I didn't really know what to look for, anyways take a look at this picture:
See how the "Failed" in the Status column has a blue outline around it, indicating that it was the most recently selected/the mouse was over it when the user stopped dragging. I want to either be able to: 1) disallow the JTable to have a single last-selected-element or 2) be able to set it myself. How do I do that?
If it helps, the reason I want either one of those is that I am refreshing the table which means I need to reapply the selected rows, however when I do so this last-selected-element thing gets lost.
Edit: I would actually really rather disallow the JTable from having a "lead selection index" as it is called, how do I do that?
That cell indicates which cell has focus as indicated by the following methods:
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
I want to either be able to set it myself
You can set the selected cell and selected rows with code like:
table.changeSelection(6, 2, false, false);
ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(3, 6);
or
ListSelectionModel lsm = table.getSelectionModel();
lsm.setSelectionInterval(3, 6);
table.changeSelection(6, 2, false, true);
Edit:
It appears you need to get the row/column from the appropriate selection model:
int row = table.getSelectionModel().getLeadSelectionIndex();
int column = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
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);
If I have a JTable in java and if I click on the first row then shift+click lets say on the 10th row, how would get all the selected rows between 1 and 10... Is there a code for it? thanks in advance
how would get all the selected rows
Check out the JTable API. You can use the getSelectedRows() method to get the indexes of all the selected rows. Then you write a loop to iterate through the indexes to access the data that you need.
I believe what you're asking for is the method JTable.setSelectionMode(int), using as parameter ListSelectionModel.SINGLE_INTERVAL_SELECTION or possibly ListSelectionModel.MULTIPLE_INTERVAL_SELECTION, to enable the selection of multiple contiguous rows. For instance:
JTable table = ...;
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
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.
I couldn't understand the difference between multiple_selection_interval and single_interval_selection in JTable.
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
next, is
table.setSelectionMode(ListSelectionModel.MULTIPLE_SELECTION_INTERVAL);
What's the difference these?
With ListSelectionModel.SINGLE_SELECTION you can just select one row.
With ListSelectionModel.SINGLE_INTERVAL_SELECTION you can select more than one row, which are in one block. (e.g. you can select row 1 - 5, but not row 1-3 and row 4-6, therefor you need ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)