1
当我在jtable中选择行时,它始终返回-1
作为表行索引。jTable索引不对
这是我的数据填写到JTable中:
private void addResultToRecTable(DefaultTableModel dtm, ArrayList<RecipeModule> al) {
if (al != null) {
int rowCount = dtm.getRowCount();
for (int i = 0; i < rowCount; i++) {
dtm.removeRow(0);
}
for (int i = 0; i < al.size(); i++) {
String[] recArray = {al.get(i).getRecID(), al.get(i).getRecName(), al.get(i).getRecCategory(), al.get(i).getRecQty(), al.get(i).getRecUnit()};
dtm.addRow(recArray);
}
}
这是我如何将数据传递到addResultToRecTable()
方法:
ArrayList<RecipeModule> al = RecipeController.searchRecipes(txtRepSearchName.getText(), txtRecSearchCate.getText());
addResultToRecTable(recDefaultTableModel, al);
我的数据填到表后。我在表格中选择了一行并打印出选定的行索引。然后我得到这个错误。
private void tableRecSearchKeyReleased(java.awt.event.KeyEvent evt) {
try{
int selectedRow = tableRecIngredients.getSelectedRow();
System.out.println(selectedRow);
}
但我得到这个错误:
java.lang.ArrayIndexOutOfBoundsException: -1
我不知道是什么问题。有什么建议?
您可以编辑与异常堆栈跟踪你的问题,让人们可以看到异常是怎么回事? –
你为什么不选择从点击的行? –