Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4275892

DefaultStyledDocument cannot be serialized when UndoManager is added as listener

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs



      Name: krT82822 Date: 09/27/99


      When attempting to serialize a DefaultStyledDocument, I receive
      the following exception:

      java.io.NotSerializableException: javax.swing.event.DocumentEvent$EventType
      at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:845)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:342)
      at java.io.ObjectOutputStream.outputClassFields(ObjectOutputStream.java:1567)
      at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:453)
      at java.io.ObjectOutputStream.outputObject(ObjectOutputStream.java:911)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:342)
      at java.io.ObjectOutputStream.outputArray(ObjectOutputStream.java:811)
      at java.io.ObjectOutputStream.checkSubstitutableSpecialClasses(ObjectOutputStream.java:432)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:337)

      I left out the rest of the stack trace as it is not relevant.

      I determined that the problem was caused by adding an instance
      of UndoManager to the listeners on the document.

      It is attempting to serialize the document, including all of its
      listeners, and fails when it reaches DocumentEvent$EventType.
      If the UndoManager is removed from the listener list, the document
      serializes without a problem. It seems to me as though the
      listeners should be listed as transient, as I can think of no
      practical reason to want to serialize the listeners.



      ------------------ revised test case (9/21/99): ------------------------------------------------

      /*

      In order to test this now, do the following:

      1) Start the application.
      2) Click on the JTextPane
      3) Type some text into the JTextPane
      4) Click the save button.

      */


      import javax.swing.*;
      import javax.swing.event.*;
      import javax.swing.text.*;
      import java.awt.event.*;
      import java.awt.*;
      import java.io.*;
      import javax.swing.undo.*;

      public class TestSave extends JFrame
      {
              JTextPane textPane = new JTextPane();
              private UndoManager iUndoManager = new UndoManager( );
              
              public TestSave( )
              {
                      Container content = getContentPane();
                      JPanel topPanel = new JPanel();
                      
                      JButton saveButton = new JButton( "Save" );
                      saveButton.addActionListener( new ActionListener() {
                                      public void actionPerformed( ActionEvent e )
                                      {
                                              save();
                                      }
                              } );
                              
                      JButton loadButton = new JButton( "Load" );
                      loadButton.addActionListener( new ActionListener() {
                                      public void actionPerformed( ActionEvent e )
                                      {
                                              load();
                                      }
                              } );
                      
                      JButton clearButton = new JButton( "Clear" );
                      clearButton.addActionListener( new ActionListener() {
                                      public void actionPerformed( ActionEvent e )
                                      {
                                              clear();
                                      }
                              } );
                      
                      topPanel.add( saveButton );
                      topPanel.add( loadButton );
                      topPanel.add( clearButton );
                      
                      content.add( topPanel, BorderLayout.NORTH );
                      content.add( textPane, BorderLayout.CENTER );
                      textPane.getDocument().addUndoableEditListener( iUndoManager );
              }
              
              public void save( )
              {
                      try
                      {
                              ObjectOutputStream out =
                                      new ObjectOutputStream(
                                              new FileOutputStream( "d:\\test.bin" )
                                      );
                      
                              out.writeObject( textPane.getDocument() );
                      } catch ( Exception exc )
                      {
                              exc.printStackTrace( System.out );
                      }
                      
              }
              
              public void load( )
              {
                      try
                      {
                              ObjectInputStream in =
                                      new ObjectInputStream(
                                              new FileInputStream( "d:\\test.bin" )
                                      );
                              textPane.setDocument( (Document) in.readObject( ) );
                      } catch ( Exception exc )
                      {
                              System.out.println( "Exception: " + exc );
                      }
              }
              
              /*
                      public void clear()
                      {
                              textPane.setDocument( new DefaultStyledDocument() );
                      }
              */

              public void clear() {
                      try {
                              Document doc = textPane.getDocument();
                              doc.remove( 0, doc.getLength() );
                      }
                      catch ( Exception exc ) {
                      }
              }

              
              public static void main( String[] args )
              {
                      TestSave test = new TestSave();
                      test.setSize( 600, 600 );
                      test.addWindowListener( new WindowAdapter() {
                                      public void windowClosing( WindowEvent e )
                                      {
                                              e.getWindow().dispose();
                                              System.exit( 0 );
                                      }
                              } );
                   test.setVisible( true );
              }
      }

      (Review ID: 95612)
      ======================================================================

            tprinzing Tim Prinzing (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: