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

Cannot serialize JButton that was configured from an Action

XMLWordPrintable

    • x86
    • windows_nt, windows_xp

      Name: jk109818 Date: 08/12/2003


      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      When a JButton, created through the constructor that takes a single Action argument, is passed to ObjectOutputStream.writeObject, a NotSerializableException is thrown.

      It seems that the field actionPropertyChangeListener should be declared transient or that the class javax.swing.AbstractActionPropertyChangeListener should be serializable (probably the former).

      This is illustrated by the following piece of code:

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

      class Test2 implements Serializable {

      private JFrame frame = new JFrame();

      private JButton button = new JButton(new AbstractAction("Save & Exit") {
      public void actionPerformed(ActionEvent _) {
      try {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("saved"));
      oos.writeObject(button);
      oos.close();
      } catch (Exception e) {
      System.out.println(e);
      }
      System.exit(1);
      }
      });

      Test2() {
      frame.getContentPane().add(button);
      frame.pack();
      frame.setVisible(true);
      }

      public static void main(String[] args) {
      new Test2();
      }
      }


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.io.NotSerializableException: javax.swing.AbstractButton$ButtonActionPropertyChangeListener

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      Use a constructor for the JButton not based on an Action and manually set a listener on it. This solution is not acceptable to us however as we have other issues involving buttons/actions. The workaround is illustrated with the code below:

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

      class Test2 implements Serializable {

      private JFrame frame = new JFrame();

      private JButton button = new JButton("Save & Exit");

      protected ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent _) {
      try {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("saved"));
      oos.writeObject(button);
      oos.close();
      } catch (Exception e) {
      System.out.println(e);
      }

      System.exit(1);
      }
      };

      Test2() {
      frame.getContentPane().add(button);
      frame.pack();
      frame.setVisible(true);

      button.addActionListener(actionListener);
      }

      public static void main(String[] args) {
      new Test2();
      }
      }
      (Incident Review ID: 186247)
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: