英文:
How to change all column values In JTable?
问题
我正在尝试在Java中更改表格的值,但我无法弄清楚如何做到这一点。根据下面的图像,我想将所有列的状态值更改为"pending"。您可以参考我编写的代码(但目前它不起作用):
DefaultTableModel model = (DefaultTableModel)DispatchTable.getModel();
// 将所有Status行设置为pending
model.setValueAt("pending", 4, model.getColumnCount());
我不确定我在这里做错了什么,但它不允许我的应用程序工作,我认为也许我必须创建一个for循环并在循环中手动更改所有值?请告诉我一个解决方案,谢谢。
英文:
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)
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.
答案1
得分: 3
以下代码将更改“Status”列的所有单元格值。
for (int row = 0; row < dispatchTable.getRowCount(); row++) {
dispatchTable.setValueAt("Pending", row, dispatchTable.getColumn("Status").getModelIndex());
}
英文:
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论