0

I searched but there are no similar questions.

I have a function with 2 parameters: row_num, value Before adding a new value, I need to check to see if that row exists in the JTable or not. If it is not, I will add a null row before adding value.

The problem is I don't know how to check if it exists. I tried table.isCellEditable(row_num, 2) but I'm not sure why it always returns True though the table doesn't contain that row.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Louis Tran
  • 1,154
  • 1
  • 26
  • 46
  • *"I tried `table.isCellEditable(row_num, 2)` but I'm not sure why it always returns `True` though the table doesn't contain that row."* Did you try Voodoo? That's just as likely to work as using methods at random, without understanding how they work or what they are actually supposed to do. For that last part, try reading the Java Docs. – Andrew Thompson Mar 19 '17 at 05:21
  • Possible duplicate of [*Store products in a TreeSet and print the content in a JTable*](http://stackoverflow.com/q/29582246/230513); see `TreeSet:add` for more. – trashgod Mar 19 '17 at 07:48

1 Answers1

1

There's not much to go on here. Just check the table row count:

DefaultTableModel dtm = (DefaultTableModel) yourTable.getModel();
int rows = dtm.getRowCount();

If the value in row_num is greater than rows then the row obviously doesn't exist yet. If row_num is working off of the JTable index value which is 0 based then you would need to subtract 1 from rows or:

if (row_num < rows) { 
    System.out.println("Yup...The row number " + row_num + " does exist!");
    // Call your specific method here....
}

Or is it that you are trying to compare a value contained within a row specific row cell in which case we would also need the column number of that specific cell. Maybe I'm just not grasping your question properly and I have this all wrong in which case I'll simply delete this answer.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22