我正在创建java可编辑表,并遇到问题:它"崩溃"读完数据后。这是我的表:
table.getTableHeader().setReorderingAllowed(false);
tableModel = new DefaultTableModel(new Object[]
{"#","1","2","3","4","5","6","7"},8);
table.setModel(tableModel);
table.getColumnModel().getColumn(0).setMaxWidth(22);
从txt阅读:
OK4.addActionListener(new ActionListener(){
@Override
File f = new File(fileName);
if(f.exists()){
try {
tableModel = new DefaultTableModel(new Object[]{"#","1","2","3","4","5","6","7"},0);
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
String[] colHeaders = line.split("\\s+");
tableModel.setColumnIdentifiers(colHeaders);
while ((line = br.readLine()) != null) {
String[] data = line.split("\\s+");
tableModel.addRow(data);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(null, "this day is not saved");
};
table.setModel(tableModel);
table.getColumnModel().getColumn(0).setMaxWidth(22);
});
导致我的总和字段的主要问题:
OK3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
int sum=0;
int number;
Object smth;
String Smth;
int kint2 = table.getRowCount()-1;
if(table.getValueAt(kint2, 1)=="total:"){
}else{
tableModel.addRow(new Object[]{});
int a = table.getRowCount()-1;
table.setValueAt("total:", a, 1);
}
for (int j = 2; j < 8; j++) {
for (int i = 0; i < table.getRowCount()-1; i++) {
smth = table.getValueAt(i,j);
Smth = (String) smth;
if (smth==null){
number=0;
}else{
number=Integer.parseInt(Smth);
}
sum=sum+number;
}
table.setValueAt(sum, table.getRowCount()-1, j);
sum=0;
}
}
});
应用程序在从txt读取后停止计算总和或仅计算前4行总和。是否与我的tableModel
有关?
感谢您的回答。