将二维字符串传递给JTable
问题描述:
我试图从CSV文件中获取数据,将其解析为二维数组,然后将其返回给显示在JTable中的GUI。似乎不太好!将二维字符串传递给JTable
方法从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;
}
}
CODE FOR GUI
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,因为我必须为此使用默认库。
答
把String tempArray[][] as JTables构造JTable(Object[][] rowData, Object[] columnNames)直接
更好的办法应该是一个新的行添加到
TableModel
,而不是取代JTable中运行时,更多的教程Creating a Table Model
+0
谢谢,我会研究表模型。似乎它会更适合。 –
1)* “显示有问题。”*有什么问题? 2)为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)。 3)不需要多于一行的空白空白。 –
麻烦,因为它不显示它或任何东西。该按钮似乎没有做任何事情,并且监听器处于活动状态。 –
您在重新载入操作中没有对“数据”执行任何操作。将其填充到JTable的表或TableModel。 – vels4j