如何更改JTable中的所有列值?

huangapple go评论82阅读模式
英文:

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) 如何更改JTable中的所有列值?

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 &lt; dispatchTable.getRowCount(); row++) {
    dispatchTable.setValueAt(&quot;Pending&quot;, row, dispatchTable.getColumn(&quot;Status&quot;).getModelIndex());
}

huangapple
  • 本文由 发表于 2020年7月30日 18:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63170945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定