Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Table update problems

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Table update problems

    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!
    Last edited by Sedu; August 18th, 2014 at 01:46 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Table update problems

    You're reading the value before the table model is refreshed or updated. This question was addressed recently in another thread, but I can't find it, and I don't remember the precise solution. You might try searching the web for similar terms.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Table update problems

    Actually, that was likely me as well. I had a similar problem with updating the scroll bar. The solution to that was to use invokeLater (the reason I mentioned it initially). I'm looking around a bit more online, but if there were any way to force an update before I continued, that would probably be ideal. In any event, thanks for the response!

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Table update problems

    So I have tried all of the below to get the data model to refresh, but the only thing I can find that will make the model's data (or the data that the table itself returns) match what is on the screen is to select a different cell in the table.
        int i = <THE ROW IN QUESTION>;
     
        procTableModel.fireTableDataChanged();
        procTableModel.fireTableRowsUpdated(i, i);
        tblProcGuide.repaint();
     
     
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                List<PronunciationNode> newPro = new ArrayList<PronunciationNode>();
     
                for (int i = 0; i < procTableModel.getRowCount(); i++) {
                    PronunciationNode newNode = new PronunciationNode();
     
                    // THIS IS THE AREA WHERE I DON'T SEE UPDATED VALUES
                    newNode.setValue(procTableModel.getValueAt(i, 0).toString());
                    newNode.setPronunciation(procTableModel.getValueAt(i, 1).toString());
     
                    newPro.add(newNode);
                }
     
                core.setPronunciations(newPro);
            }
        });

    I'm still looking for any solutions if you guys have suggestions. It is just a frustratingly small bug that it has been frustratingly difficult to quash.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Table update problems

    I found a solution to this. It's a little bit hackey, but it works. Essentially, I just pull the value from the cell editor rather than the model, which is... not ideal, but works. The fix is below, and has to do only with the reading of date, not to do with delaying its execution, or with updating the model.

    for (int i = 0; i < tblProcGuide.getRowCount(); i++) {
                PronunciationNode newNode = new PronunciationNode();
     
                newNode.setValue((String) tblProcGuide.getCellEditor(i, 0).getCellEditorValue());
                newNode.setPronunciation((String) tblProcGuide.getCellEditor(i, 1).getCellEditorValue());
     
                newPro.add(newNode);
            }

Similar Threads

  1. adding data in table but the table is in another frame
    By brianskiee in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 13th, 2014, 03:31 PM
  2. Update problem
    By dannynoogen in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 23rd, 2013, 03:58 PM
  3. [SOLVED] Problem with update table after inserting new values
    By justyStepi in forum JDBC & Databases
    Replies: 4
    Last Post: September 10th, 2012, 03:52 PM
  4. Update table in database
    By CTheSky in forum JDBC & Databases
    Replies: 4
    Last Post: February 24th, 2011, 02:02 AM