Tutorial :Getting real row indices of selected rows in JTable when columns are alphabetized


If the columns of my JTable are unalphabetized, I can use getSelectedRows() and get values of their rows without any trouble. But, if the user clicks on the column name and the rows are alphabetized in that column, getSelectedRows() returns not the currently selected rows, but the rows that were originally there before alphabetization.

How can I get the currently selected rows when the columns are alphabetized?


Use this code you will get the correct rows you visually selected.

int[] row_indexes=jTable1.getSelectedRows();  for(int i=0;i<row_indexes.length;i++){    domain=jTable1.getValueAt(row_indexes[i], 1).toString();      System.out.println(this, domain);  }  


private void selectRow() {    //retrieving the selected row index    int row = jTable1.getSelectedRow();    //if a single row is selected from the table, take each cell values into the controls     if (jTable1.getRowSelectionAllowed())   {       selectedJobId = Integer.parseInt(jTable1.getValueAt(row, 0).toString());       jTextField_JobName.setText(jTable1.getValueAt(row, 1).toString());       jTextField_ExpDate.setText(jTable1.getValueAt(row, 3).toString());       jComboBox_JobCat.setSelectedItem(jTable1.getValueAt(row, 4).toString());       jComboBox_JobSubCat.setSelectedItem(jTable1.getValueAt(row, 5).toString());     }    }  


Probably a bit late, but thought I'd post this anyway.

Have a look at the JTable method convertRowIndexToModel(row). It returns the row number as if the row number hasn't been sorted.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »