如果我的JTable中的列没有按字母顺序排列,我可以使用getSelectedRows()并轻松地获取它们行的值。但是,如果用户单击列名,并且该列中的行按字母顺序排列,则getSelectedRows()返回的不是当前选定的行,而是字母排列前最初存在的行。
当列按字母顺序排列时,如何获取当前选定的行?
发布于 2011-06-17 13:17:01
使用此代码,您将获得直观地选择的正确行。
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);
}发布于 2012-11-16 12:32:52
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());
}
}发布于 2009-05-13 14:56:11
可能有点晚了,但我想我还是把这个贴出来吧。
看一下row方法convertRowIndexToModel( JTable )。它返回行号,就好像行号还没有排序一样。
https://stackoverflow.com/questions/649300
复制相似问题