我正在开发一个从 csv 文件读取的项目,然后使用 StringTokenizer 来分割元素并将它们放入 JLabels 中。到目前为止,我已经完成了那部分。我有按钮可以滚动浏览每个位置,但我不确定如何在字段中键入并添加到数组中?
到目前为止,这是我程序的主要部分
// program reads in csvfile.
private void loadCarList() {
try{
BufferedReader CSVFile = new BufferedReader(new FileReader("car.txt"));
String dataRow = CSVFile.readLine();
while(dataRow != null){
carList.add(dataRow);
dataRow = CSVFile.readLine();
}
}catch(Exception e){
System.out.println("Exception while reading csv file: " + e);
}
}
}
//this will click cycle through the elements in the Jlabels.
private void loadNextElement(){
try {
StringTokenizer st = new StringTokenizer((String)carList.get(position), ",");
while(st.hasMoreTokens() && position <= carList.size() -1) {
position ++;
String CarID = st.nextToken();
String DealerShipID = st.nextToken();
String ColorID = st.nextToken();
String Year = st.nextToken();
String Price = st.nextToken();
String Quantity = st.nextToken();
tCarID.setText(CarID);
tDealerShip.setText(DealerShipID);
tColor.setText(ColorID);
tYear.setText(Year);
tPrice.setText(Price);
tQuantity.setText(Quantity);
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, "youve reached the end of the list");
}
}
有没有一种更简单的方法,我可以输入我布置的 jlabels 并添加到数组中?
在这一点上我有点迷茫,我不确定如何进一步处理。