I am having trouble with a jTable that I am using. I have set the listeners of the cells to save the information of the entire table every time they are modified, but run into the problem that the last modified cell does will not reflect its most recent value. The value will display visually, but not show up when I try to read from the cell. I have tried wrapping the saveProcGuide() call in swing's invokelater, but to no avail. I have marked the area where the problem becomes evident (values returned do not match those present in the visible table object). Below is the offending code:
// This is where I set up the listeners private void addProcGuideWithValues() { procTableModel.addRow(new Object[]{"", ""}); // document listener to be fed into editor/renderers for cells... DocumentListener docuListener = new DocumentListener() { public void changedUpdate(DocumentEvent e) { saveProcGuide(); } public void removeUpdate(DocumentEvent e) { saveProcGuide(); } public void insertUpdate(DocumentEvent e) { saveProcGuide(); } }; // set saving properties for first column editor TableColumnEditor editor = (TableColumnEditor) tblProcGuide.getCellEditor(procTableModel.getRowCount() - 1, 0); editor.setDocuListener(docuListener); // set saving properties for second column editor editor = (TableColumnEditor) tblProcGuide.getCellEditor(procTableModel.getRowCount() - 1, 1); editor.setDocuListener(docuListener); } // This is the method where the incorrect values are saved private void saveProcGuide() { List<PronunciationNode> newPro = new ArrayList<PronunciationNode>(); for (int i = 0; i < procTableModel.getRowCount(); i++) { PronunciationNode newNode = new PronunciationNode(); // THIS IS THE PROBLEM -> newNode.setValue(procTableModel.getValueAt(i, 0).toString()); newNode.setPronunciation(procTableModel.getValueAt(i, 1).toString()); // <- THIS IS THE PROBLEM newPro.add(newNode); } core.setPronunciations(newPro); }
Any help here would be much appreciated!