Ok, I have an interesting issue that I'm sure I'm just overlooking things on.
I have a JTable with several rows and columns. The user can make changes to each cell, move onto a different JTable, and then reload the previously edited JTable to make additional changes or whatever. My current issue is restricted to one of the columns not saving its data. When the JTable is reloaded, only that column has returned to its default values. Each JTable is loaded by a JList that contains ChartInfo Objects. These ChartInfo Objects contain the values to be placed in the JTable when the JTable is loaded.
This problem only currently exists in column 2. The problem does not exist in column 1, which works practically the same way. Column 2 contains data known as "Range".
A ListSelectionListener is used to load up the JTables. Here is the part of the ListSelectionListener that is responsible for setting the data into the JTable. At this point, when the user chooses to reload the JTable, the value of tempIn.range is set back to its default value:
public class ListListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { ChartInfo temp = (ChartInfo)list.getSelectedValue(); if(temp!=null) { int rowCount = temp.series.size(); data = new Object[rowCount][5]; for(int i=0;i<rowCount;i++) { SeriesInfo tempIn = temp.series.get(i); data[i][0] = tempIn.name; data[i][1] = tempIn.range; System.out.println("Temp Chart Range: "+tempIn.range); data[i][2] = tempIn.primeAxis; data[i][3] = tempIn.color; data[i][4] = tempIn.dataName; }
This is the code for the DefaultCellEditor attached to the cells in Column 2. The editingStopped method is supposed to set the value in the JTable, set the value in the JTable's data array, and set the value in the ArrayList that contains all of the ChartInfo Objects. By the time it finishes this method, the edited values have been correctly set in the edited ChartInfo Object:
DefaultCellEditor cellEditor1 = new DefaultCellEditor(new JTextField()); cellEditor1.setClickCountToStart(1); cellEditor1.addCellEditorListener(new CellEditorListener(){ public void editingCanceled(ChangeEvent e) { System.out.println("Editing Canceled: "+e); } public void editingStopped(ChangeEvent e) { table.setValueAt(((DefaultCellEditor)e.getSource()).getCellEditorValue(),table.getSelectedRow(),1); data[table.getSelectedRow()][1] = table.getValueAt(table.getSelectedRow(),1); System.out.println("Value at Table Array: "+data[table.getSelectedRow()][1]); int tempNum = list.getSelectedIndex(); Point[] pointsArr = ref.getPoints(table.getValueAt(table.getSelectedRow(),1).toString()); System.out.println("Value at Table: "+table.getValueAt(table.getSelectedRow(),1).toString()); System.out.println("Start Value: "+pointsArr[0]); System.out.println("End Value: "+pointsArr[1]); chartArray.get(tempNum).series.get(table.getSelectedRow()).start = pointsArr[0]; chartArray.get(tempNum).series.get(table.getSelectedRow()).end = pointsArr[1]; System.out.println("Chart Array Start Value: "+chartArray.get(tempNum).series.get(table.getSelectedRow()).start); System.out.println("Chart Array End Value: "+chartArray.get(tempNum).series.get(table.getSelectedRow()).end); } }); table.getColumnModel().getColumn(1).setCellEditor(cellEditor1);
To give a comparison of how it works, here is the DefaultCellEditor for column 1, which is not suffering from any issues of storing data:
DefaultCellEditor cellEditor = new DefaultCellEditor(new JTextField()); cellEditor.setClickCountToStart(1); cellEditor.addCellEditorListener(new CellEditorListener(){ public void editingCanceled(ChangeEvent e) { System.out.println("Editing Canceled: "+e); } public void editingStopped(ChangeEvent e) { table.setValueAt(((DefaultCellEditor)e.getSource()).getCellEditorValue(),table.getSelectedRow(),0); data[table.getSelectedRow()][0] = table.getValueAt(table.getSelectedRow(),0); int tempNum = list.getSelectedIndex(); chartArray.get(tempNum).series.get(table.getSelectedRow()).name = table.getValueAt(table.getSelectedRow(),0).toString(); } }); table.getColumnModel().getColumn(0).setCellEditor(cellEditor);
Those are the only two things that should be editing values for the ChartInfo Objects. For reference, here is the ChartInfo Object Class and the SeriesInfo Object Class.
ChartInfo Object Class:
SeriesInfo Object Class:import java.util.ArrayList; public class ChartInfo { String title; String LALabel; String RALabel; String BALabel; String dataRange; String catRange; String type; ArrayList<SeriesInfo> series; public ChartInfo(String data,String cat,String ti,String la,String ra,String ba,String ty,ArrayList<SeriesInfo> arr) { dataRange = data; title = ti; LALabel = la; RALabel = ra; BALabel = ba; type = ty; series = arr; catRange = cat; } public String toString() { if(!title.equals("")) return title; return "Graph: "+dataRange; } }
import com.smartxls.WorkBook; import java.awt.Point; import java.awt.Color; public class SeriesInfo { String name; Point start; Point end; String dataName; WorkBook workBook; String range; Color color; boolean primeAxis; public SeriesInfo(String n,Point s,Point e,String d) { name = n; start = s; end = e; dataName = d; try{ workBook = new WorkBook(); range = workBook.formatRCNr((int)start.getX(),(int)start.getY(),false)+":"+workBook.formatRCNr((int)end.getX(),(int)end.getY(),false); }catch(Exception ex){System.out.println("SeriesError: "+ex); } primeAxis = true; //Delete color = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)); } public SeriesInfo(String n,Color colour,Point s,Point e,String d) { name = n; start = s; end = e; dataName = d; try{ workBook = new WorkBook(); range = workBook.formatRCNr((int)start.getX(),(int)start.getY(),false)+":"+workBook.formatRCNr((int)end.getX(),(int)end.getY(),false); }catch(Exception ex){System.out.println("SeriesError: "+ex); } primeAxis = true; color = colour; } }
And here is the Reference.getPoints(String) method, and the methods used in that method, that is used in the DefaultCellEditor:
public Point[] getPoints(String value) { Point[] points = new Point[2]; String value1 = value.substring(0,value.indexOf(":")); String value2 = value.substring(value.indexOf(":")+1); points[0] = new Point(getRowIndex(value1),getColumnNumber(value1)); points[1] = new Point(getRowIndex(value2),getColumnNumber(value2)); System.out.println("Point 1: Row: "+points[0].getX()+" Column: "+points[0].getY()); System.out.println("Point 2: Row: "+points[1].getX()+" Column: "+points[1].getY()); return points; } public int getRowIndex(String value) { return getRow(value)-1; } public int getColumnIndex(String value) { String[] letters = new String[] {"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; int ans = 0; String col = value; for(int i=0;i<col.length();i++) { int count = col.length(); String temp = col.substring(i,i+1); for(int x=0;x<letters.length;x++) { if(temp.equals(letters[x])) { int tempCount = count-i; ans += (Math.pow(26,tempCount-1)*x); break; } } } return ans-1; }
If anything is unclear, please ask and I'll try to clear it up. I've tried to reduce the code I've given to only what is relevant for my issue.