I have a JTable with 10 rows. If I have multiple selections on the JTable, how do I get the cell that is currently focused?
I have tried to used JTable's getEditingColumn() and getEditingRow(), but they always return -1 during multiple selection.
The commands;
table.getSelectionModel().getLeadSelectionIndex();
table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
give the selected row and column respectively.
Related
I'm working on a java desktop application and I have a Swing JTable for displaying data. I need to have a column for displaying row index for each row in the table. I don't want to add index in the grid data. Here is the way that I'm using to fill my table:
private DefaultTableModel defaultTableModel = new DefaultTableModel();
defaultTableModel.setDataVector(dataArray, headerArray);
jTable.setModel(defaultTableModel);
JTable displays what the TableModel tells it to display. Instead, extend AbstractTableModel. In your implementation of getValueAt(), return the row number for column zero and return the appropriate element of dataArray otherwise. Related examples may be found here and here.
You can add a row header to the scroll pane to display numbers for each row in the table.
Any component can be added to the row header. So one solution is to add a second (custom) JTable that just displays row numbers with a custom renderer so that the number look like the column headers.
Check out Row Table Number for an example of this approach.
I have an Eclipse SWT table. It contains multiple rows (with row headers) and multiple columns (with column headers). On click on a column header I want to deselect all rows and highlight only the column's header (or all cells of this column). Is that possible?
I already registered a listener for the column header's selection and am able to set the selection to the given column, but then it also always selects the first row automatically. I tried with SWT.FULL_SELECTION and SWT.SINGLE as the style of my table, but it only changes the way how the row is highlighted, but I want only to highlight the column's header (or optionally all cells of this column) and no rows.
Is it possible? Did anyone have the same problem?
I ran into the same problem and I'm afraid you can't do that in SWT.
I solved by imitating the selection of a column; I colored the cells as if they were selected, and dropped the "real" selection.
You should extend your own TableViewer and override the getSelection() method to reflect that you now also have this imitated kind of selection.
(And from a UX perspective, you should make sure that a copy command (i.e. Ctrl+C) behaves as users expect it.)
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.
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);
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.