这是我从csv-file获取数据的方式:
cSVFileReader = new CSVReader(new FileReader(sciezka), ','); // csv reader with coma-separator
java.util.List<String[]> myEntries = cSVFileReader.readAll();
String[][] rowData = myEntries.toArray(new String[0][]);
rowData = myEntries.toArray(new String[0][]);
columnnames = myEntries.get(0);
rowData = myEntries.toArray(new String[0][]);
DefaultTableModel tableModel = new DefaultTableModel(rowData, columnnames);
JTable table = new JTable(tableModel);
return table;
这就是我的平均值:
public void getAverage() throws IOException{
CSVFile table = new CSVFile() ;
float sum = 0;
DefaultTableModel model = (DefaultTableModel) table.getModel();
int column = table.getSelectedColumn();
System.out.println(column); //show -1
rowcount = model.getRowCount();
System.out.println(rowcount); //show zero
}
我认为,问题在于从JTable获取TableModel是错误的,但实际上我无法理解如何以另一种方式实现它。
答案 0 :(得分:1)
所以最后它有效。我的解决方案是table,它作为参数传递
public float getAverage(JTable table) throws IOException