Getting real row indices for selected rows in JTable if columns are listed in alphabetical order

If the columns of my JTable are not sorted, I can use getSelectedRows () and get the values ​​of my rows without any problems. But, if the user clicks on the column and row name in alphabetical order in that column, getSelectedRows () returns not the currently selected rows, but the rows that were originally there before the alphabet.

How can I get the currently selected rows when the columns are in alphabetical order?

+3
source share
3 answers

Use this code, you will get the correct lines that you have 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);
}
+3
source
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());

 }

}
+2
source

, , , .

JTable convertRowIndexToModel (). , .

+1

Source: https://habr.com/ru/post/1704805/


All Articles