0

I am trying to change the value of my table in Java but I cannot figure out how to do so. Based on the image below, I want to change all the column values of status to be "pending" You can refer to the code that I written (but its not working at the moment) enter image description here

DefaultTableModel model = (DefaultTableModel)DispatchTable.getModel();
// Set all Status rows to pending
model.setValueAt("Dispatched", 4, model.getColumnCount());

Im not sure what I'm doing wrong here but its not allowing my application to work, I think maybe I have to create a for loop and manually change all the values in the loop? Do tell me of a solution, thank you.

  • I'm not sure I fully understand your question, but is this a possible duplicate ? https://stackoverflow.com/questions/3179136/jtable-how-to-refresh-table-model-after-insert-delete-or-update-the-data – TheYaINN Jul 30 '20 at 10:21
  • What im saying is that for example, if I have 10 rows, I want to change all 10 rows under the "status" column to pending, but I do not know how to do that. – Jackson Etherchain Stake Jul 30 '20 at 10:39
  • Method `setValueAt()` in interface `TableModel` is the way to change the data displayed by the `JTable`. Can you be more specific about your actual problem? I don't understand what you mean by _not allowing my application to work_ – Abra Jul 30 '20 at 10:55
  • `model.setValueAt("Dispatched", 4, model.getColumnCount());` should throw `ArrayIndexOutOfBoundsException` because, according to the image in your question, `getColumnCount()` returns 5 but the index of column _Status_ is 4. – Abra Jul 30 '20 at 10:59

1 Answers1

3

Following code will change all the cell values of 'Status' column.

for (int row = 0; row < dispatchTable.getRowCount(); row++) {
    dispatchTable.setValueAt("Pending", row, dispatchTable.getColumn("Status").getModelIndex());
}
Arindam Roy
  • 111
  • 2