我有一个程序,可以让您将课程添加到JTable的一周日历中,我希望它以某种方式工作,当您单击“保存”按钮时,日历会保存到一个.txt文件中,然后在启动时再次使用该应用程序,它将把保存的课程加载到JTable中。我遇到的问题是,当保存表时,将空的单元格保存为null,而将它们加载时,空单元格将显示为null,我希望它加载为空。
我已经尝试过使用嵌套的for循环将所有空值设置为“”,但它只是将完整的JTable显示为空白。
"Below is code to save full table into .txt file"
public void actionPerformed(ActionEvent e)
{
try {
PrintWriter outputFile = new PrintWriter("C:\\Users\\Shervin\\eclipse-workspace\\GUIDemo2\\CourseSchedule.txt");
for(int i = 0;i<table.getRowCount();i++)
{
for (int j= 0;j<table.getColumnCount();j++)
{
outputFile.print(table.getValueAt(i, j));
outputFile.print(",");
}
outputFile.println();
}
JOptionPane.showMessageDialog(null, "Schedule has been saved...");
outputFile.close();
}
catch (FileNotFoundException e2) {
e2.printStackTrace();
}
}
});
"Below is the code to load table from txt.file back into JTable"
private void loadInto()
{
try {
Scanner scanner = new Scanner(new BufferedReader(new FileReader("C:\\Users\\Shervin\\eclipse-workspace\\GUIDemo2\\CourseSchedule.txt")));
while(scanner.hasNextLine())
{
for(int i = 0;i<table.getRowCount();i++) {
String[] line = scanner.nextLine().trim().split(",");
for (int j = 0;j<table.getColumnCount();j++)
{
table.setValueAt(line[j], i, j);
}
}
}
scanner.close();
}catch (IOException ioexception) {}
}