【发布时间】:2012-10-16 08:35:53
【问题描述】:
我正在尝试从 CSV 文件中获取数据,将其解析为二维数组,然后将其返回到在 JTable 中显示它的 GUI。好像不太顺利!
从 CSV 获取的方法
static String[][] readGUIFromPropertyFile(String sFileName, String userName) throws FileNotFoundException
{
String thisLine;
String tempArray[][] = new String[20][4];
int i = 0;
BufferedReader reader = new BufferedReader(new FileReader(sFileName));
try
{
while((thisLine = reader.readLine()) != null)
{
String propertyDetails[] = thisLine.split(",");
if (propertyDetails[0].equals(userName))
{
tempArray[i][0] = propertyDetails[1];
tempArray[i][1] = propertyDetails[2];
tempArray[i][2] = propertyDetails[3];
tempArray[i][3] = propertyDetails[4];
tempArray[i][4] = propertyDetails[5];
i++;
}
}
return tempArray;
}
catch(IOException e)
{
System.out.print("\nProperties do not exist\n");
e.printStackTrace();
}
finally{
try
{ reader.close();
}catch (IOException e){}}
return tempArray;
}
}
图形用户界面代码
else if (event.getSource() == reload)
{
try {
data = CSVWrite.readGUIFromPropertyFile(propertyFile, userName);
JOptionPane.showMessageDialog(frame, "Properties Loaded");
userPropertyView.add(displayProperties);
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(frame, "Properties Not Loaded");
e.printStackTrace();
}
}
我让它与命令界面一起工作,所以我知道代码正在工作,但我必须同时实现 GUI 和命令行。我可以从 GUI 写入 CSV,没问题,但无法显示它。我研究了 ArrayLists,实际上我明天有一个关于它们的讲座,所以这也是一种可能性。
我不能使用 OpenCSV,因为我必须为此使用默认库。
【问题讨论】:
-
1) “显示有问题。” 什么问题? 2) 为了尽快获得更好的帮助,请发布SSCCE。 3)不需要多于一行的空白'white-space。
-
问题在于它没有显示它或任何东西。该按钮似乎没有做任何事情,并且侦听器处于活动状态。
-
您在重新加载操作中没有对“数据”进行任何操作。将其填充到 JTable 的 table 或 TableModel 中。
-
OK.. 将该信息编辑到问题中。我什么时候可以看到第 2) 点的结果?
-
这里java-tips.org/java-se-tips/javax.swing/…是一个简单的例子,另外你可以添加监听器。
标签: java swing file-io csv jtable