为什么最后一个数据始终为null

时间:2017-10-18 08:09:06

标签: java

我在jtable中获取所有数据时遇到问题。即使我在该单元格中输入数据,显示的最后一个数据也始终为“null”。任何人都可以帮助我

for (int i = 0; i < model_1.getRowCount(); i++) {

  if (String.valueOf(model_1.getValueAt(i, 0)) != null && !String.valueOf(model_1.getValueAt(i, 0)).isEmpty()) {
    date = String.valueOf(model_1.getValueAt(i, 0));
  }
  if (String.valueOf(model_1.getValueAt(i, 1)) != null && !String.valueOf(model_1.getValueAt(i, 1)).isEmpty()) {
    meal = String.valueOf(model_1.getValueAt(i, 1));
  }
  if (String.valueOf(model_1.getValueAt(i, 2)) != null && !String.valueOf(model_1.getValueAt(i, 2)).isEmpty()) {
    time = String.valueOf(model_1.getValueAt(i, 2));
  }
  if (String.valueOf(model_1.getValueAt(i, 3)) != null && !String.valueOf(model_1.getValueAt(i, 3)).isEmpty()) {
    activity = String.valueOf(model_1.getValueAt(i, 3));
  }
  try {
    System.out.println(date + " " + meal + " " + time + " " + activity);
  }
}

这是我的示例输出:

2017-10-02 sdfsd 01:30 sdfsdfsf          
2017-10-03 dsfdfs 01:00 null

1 个答案:

答案 0 :(得分:1)

我猜您的jtable仍然有效,因此数据尚未保存到模型中。

尝试在代码之前添加:

if (table.isEditing())
     table.getCellEditor().stopCellEditing();