当列按字母顺序排列时,获取JTable中所选行的实际行索引

时间:2009-03-16 04:54:24

标签: jtable

如果我的JTable的列是非字母的,我可以使用getSelectedRows()并获取其行的值而没有任何问题。但是,如果用户单击列名称并且行在该列中按字母顺序排列,则getSelectedRows()不返回当前选定的行,而是返回字母化之前最初存在的行。

如何在按字母顺序排列列时获取当前选定的行?

3 个答案:

答案 0 :(得分:3)

使用此代码,您将获得视觉选择的正确行。

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);
}

答案 1 :(得分:2)

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 :(得分:1)

可能有点迟了,但我还是认为我会发布这个。

查看JTable方法convertRowIndexToModel(row)。它返回行号,就好像行号尚未排序一样。

相关问题