-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.3.1, 1.4.1, 1.4.2
-
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)
======================================================================
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)
======================================================================
- duplicates
-
JDK-4626632 Various containers for Action throw NPE when PropChgEvent has null newValue
- Resolved
- relates to
-
JDK-4966168 JInternalFrame not serializable in Motif & GTK L&F
- Resolved