所以我对程序语言有很多经验,我正在尝试将它映射到Java。
我有使用csv文件创建默认表模型的代码,并将文件加载到选项卡内的JTable中。它很棒。但是现在我想在表格中添加行,我无法弄清楚如何访问JTable来执行addrow。
/* Build the fight index tab from a file */
public void LoadIndexTable(String file) {
try {
FileReader fin = new FileReader(file);
DefaultTableModel imodel = createTableModel(fin, null);
JTable itable = new JTable(imodel);
indexpanel = new JScrollPane(itable);
} catch (Exception e) {
e.printStackTrace();
}
}
/ *在索引表* /
中添加一行public void AddIndexRow() {
itable.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});
}
/ *将csv文件加载到具有正确标题* /
的表中public static DefaultTableModel createTableModel(Reader in, Vector<Object> headers)
{
DefaultTableModel model = null;
Scanner s = null;
try {
Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
s = new Scanner(in);
while (s.hasNextLine()) {
rows.add(new Vector<Object> (Arrays.asList(s.nextLine().split("\\s*,\\s*",-1))));
}
if (headers == null) {
headers = rows.remove(0);
model = new DefaultTableModel(rows, headers);
} else {
model = new DefaultTableModel(rows, headers);
}
return model;
} finally {
s.close();
}
}