9 Replies Latest reply: Dec 6, 2010 2:11 PM by 821569 RSS

    Problems serializing a JTable Model - java.io.NotSerializableException

    813730
      I have a save button that tries to write a JTable model using the following line:

      obj_out.writeObject(myTableModel);

      that table model is the following declaration:

      public class MyTableModel extends AbstractTableModel implements java.io.Serializable{


      If I run the code and do not edit any values in the cells, myTableModel is written correctly. If I try to change a value and hit the save button, it throws a

      java.io.NotSerializableException


      running jdk1.6.0_18


      Any ideas?
      (I can post the code, but not sure how to do it so it's formatted correctly)

      Edited by: EJP on 13/11/2010 10:25
      Corrected the title
        • 1. Re: Problems serializing a JTable - java.io.NotSerializableException
          jtahlborn
          you would need to provide more details about your table model. obviously, the problem is not the model instance itself, but some object referenced by your model instance.
          • 2. Re: Problems serializing a JTable - java.io.NotSerializableException
            Darryl Burke
            810727 wrote:
            (I can post the code, but not sure how to do it so it's formatted correctly)
            Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

            db
            • 3. Re: Problems serializing a JTable - java.io.NotSerializableException
              DMF
              Well, it would be nice if Oracle caught up with the past and gave us a 'Code' button in the editor.

              I gotta say, if they can't even come up to the rather low standards of their predecessor forum, then these noob fumbles will continue to be inevitable. Blame Oracle, not the noob.
              • 4. Re: Problems serializing a JTable - java.io.NotSerializableException
                Darryl Burke
                Nobody's blaming anyone. And the majority of noobs ignored the CODE button on the old forums also.

                Do you wish to imply that they shouldn't  be advised to read the announcements and the FAQ?

                db

                edit Forums should have a {code} button

                Edited by: Darryl Burke
                • 5. Re: Problems serializing a JTable Model - java.io.NotSerializableException
                  813730
                  Here's more details

                  myTableModel uses a vector to add and delete rows and columns, the code looks like:
                  public class myTableModel extends AbstractTableModel implements java.io.Serializable{
                          
                          private Vector data;
                          private Vector columnNames;
                  
                          public myTableModel(){
                              data = new Vector();
                              columnNames = new Vector();
                              columnNames.add("");
                              columnNames.add(0);
                          }
                  
                     ... other methods
                  
                  }
                  the save button action works fine because I have no problems writing objects that are not instances of myTablemodel, so the problem is obviously with the table model.

                  Here is the behavior I'm seeing:
                  If rows and columns are added but nothing in the table is selected, the model is saved without issue

                  if I add data and use "enter" to move between cells, the model saves ok

                  If I add more data usng "enter" after the model has already been saved and try to save again I get:
                  java.io.NotSerializableException: com.sun.java.swing.plaf.windows.XPStyle$Skin

                  ifI move between cells using a mouse instead of "enter" I get:
                  java.io.NotSerializableException: javax.swing.JTable$CellEditorRemover


                  Some things I've tried:

                  adding the following line after the table is created:
                  myTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
                  adding the following to the save button
                  if(myTable.isEditing()){
                                      myTable.getCellEditor().stopCellEditing();
                  }
                  tried creating a deep copy of myTableModel and saving that.

                  double checked to make sure Vector implements serializeable, it does



                  I could save the values in the data fields, but I'd rather not as I need to save many other objects and this would add unnecessary complexity to the code.
                  • 6. Re: Problems serializing a JTable Model - java.io.NotSerializableException
                    jtahlborn
                    instead of trying to save your entire model, why not just save the Vector of data?
                    • 7. Re: Problems serializing a JTable Model - java.io.NotSerializableException
                      813730
                      ^ I did that and it works fine.

                      What I eventually wanted to do was save the entire JPanel that contained the table and recall it with minimal coding. So if I could serialize the entire panel, that would save me the hassle of retrieving data from every component and writing it back. So if a user wanted to continue what they were doing during the last session, instead of creating a new jpanel and filling it in with data, I could just read a jpanel object and that would be that.
                      • 8. Re: Problems serializing a JTable Model - java.io.NotSerializableException
                        jtahlborn
                        i believe most of the swing/awt classes recommend against storing them using standard java serialization. i believe the best way to serialize swing/awt stuff these days is using the XMLSerializer class. maybe you would have better luck using this instead.
                        • 9. Re: Problems serializing a JTable Model - java.io.NotSerializableException
                          821569
                          I have found that creating an inner class in the panel that uses the JTable instance in question:
                            /**
                             * This class is created only for the purpose of assisting with the 
                             * serialization of Java Swing components.  It turns out that the JTable
                             * class occasionally instantiates an inner class which is not serializable.
                             * This code gets around that problem.
                             */
                            private class KludgeTableModelListener implements TableModelListener
                            {
                          
                              /**
                               * This method sets that inner class (private member editorRemover) to null 
                               * by calling removeNotify() but then still allows you to edit the table by 
                               * calling addNotify(), which, interestingly, does not instantiate 
                               * editorRemover again.  This may have side effects of which I am unaware.
                               */
                              public void tableChanged(TableModelEvent e)
                              {
                                table.removeNotify();
                                table.addNotify();
                              }
                            }
                          and then adding it to my DefaultTableModel instance:
                          this.tableModel.addTableModelListener(new KludgeTableModelListener());
                          works well enough. Notably, if you change the field programmatically, using the table.setValueAt() function, you should then call the same methods (removeNotify() and addNotify()) on the table you just modified.

                          I had quite a bit of trouble with this. Hope this helps. Good luck!