I have a problem in retaining/keeping the values saved in a table of my GUI. My objective is to keep the values where it is stored in the table, exit my GUI, and open it again then I expect to see the same values stored in the table. My issue here is that whenever I close, and open again the GUI, it refreshes the values and makes it blank again.
I save my values by writing and reading a CSV file.
In writing the CSV file, it depends on what values the user inputs on the text fields of my GUI then it creates a CSV file on a specific location in my drive C.
In reading the CSV file, it reads the contents/values of the CSV file that was written then uses those values to store in a 2D array in order for it to be displayed into my table. But the problem is that I cannot retain those values in the table and also the table displays null values, just right next to the displayed data(I tried to do the != null but still it displays consecutive nulls just right after the inputted values)
Here is my source code:
public void writeCustomerCSV(){ // everything in this snippet code works fine(it creates a CSV file which stores the inputs of the user)
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
StringBuilder sb = new StringBuilder();
int y;
for(int x = 0; x < itemTo2D.length; x++){
for(y = 0; y < itemTo2D[0].length; y++){
if(itemTo2D[x] != null){
sb.append(itemTo2D[x][y]);
sb.append(",");
}
}
sb.append("-"); //separation for rows
sb.append(","); // separation for columns
}
bw.write(sb.toString());
bw.close();
} catch (Exception ex){
}
}
public void readCustomerCSV(){ // reads the contents of the CSV file *having issues in retaining the values
int read2DStringIndex = 0;
int newVarIndexer = 0;
String[] fromfile = {}; // 1d string for getting the columns(7 columns) of the CSV file
try{
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
String line;
while ((line = br.readLine()) != null){
fromfile = line.split(","); //separates the columns by a comma
}
} catch (Exception ex){
}
for(int g = 0; g < fromfile.length; g++){
if(fromfile[g].equals("-")){ //if there is a presence of a dash, it increments the read2DStringINdex (row index) of the 2D array
read2DStringIndex++;
newVarIndexer = 0;
}
else{
cust[read2DStringIndex][newVarIndexer] = fromfile[g]; //cust is the 2D array(declared universal) which is going to display the values to the table
newVarIndexer++;
}
}
for(int h = 0; h < cust.length; h++){ //prints cust (2D array) , just to check what data is being stored
for(int p = 0; p < cust[0].length; p++){
System.out.println(cust[h][p] + ",");
}
}
DefaultTableModel tblmodelll = (DefaultTableModel) mainTable.getModel(); // table
setrowcount = 0;
for(int r = 0; r < cust.length; r++){
if(setrowcount == 0){
tblmodelll.setRowCount(0);
}
if(cust[r][0] != null){
tblmodelll.addRow(cust[r]); //displays the cust(2D array) data to table
}
setrowcount++;
}
}
Am I missing something to add on my codes or is the logic of the code not right? You responses would indeed help in resolving this issue. Thank you very much!!!