我正在尝试使用向量从文本文件中填充表格。我应该为每一行创建一个新向量吗?我的代码还有其他问题吗?我不太确定从这里做什么。
public class myJTable {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector<String> v = new Vector<String>();
Vector<Vector<String>> rowData = new Vector<Vector<String>>();
//String splitting
try {
FileReader fReader = new FileReader("lakedata.txt");
BufferedReader inFile = new BufferedReader(fReader);
String input;
String[] temp;
while((input=inFile.readLine())!=null) {
temp = input.split(",",3);
for(int i=0; i<temp.length; i++) {
v.addElement(temp[i]);
System.out.println(temp[i]);
}
System.out.println(v);
rowData.addElement(v);
}
inFile.close();
}
catch(Exception e) {
System.out.println("ERROR");
}
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Depth");
columnNames.addElement("Temperature");
columnNames.addElement("D.O.");
JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(500,300);
frame.setVisible(true);
}
}