3

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

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

4

3 回答 3

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);
}
于 2011-06-17T05:17:00.887 回答
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());

 }

}
于 2012-11-16T04:32:52.170 回答
1

可能有点晚了,但我想我还是会发布这个。

看看 JTable 方法 convertRowIndexToModel(row)。它返回行号,就好像行号没有被排序一样。

于 2009-05-13T14:56:10.840 回答