Name: jk109818 Date: 01/31/2002
FULL PRODUCT VERSION :
java version "1.4.0-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
Java HotSpot(TM) Client VM (build 1.4.0-rc-b91, mixed mode)
FULL OPERATING SYSTEM VERSION :
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
when javax.swing.JTree is serialized using XML's long term
persistence, it is not deserialized properly. On
deserialization we get tree in it's default state.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run testcase TCJTree.java provided in Source code column
EXPECTED VERSUS ACTUAL BEHAVIOR :
Tree should be deserialize in the state in which we
serialize it, but we get only default tree every time we
deserialize it.
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.beans.*;
import javax.swing.tree.*;
import java.awt.event.*;
import java.awt.*;
public class TCJTree {
private JTree tree;
private JFrame frame;
private JScrollPane scrollpane;
private JButton btn_save, btn_retrieve;
private String filePath;
public TCJTree() {
filePath = "c:"+System.getProperties().get("file.separator")
+"tree.xml";
scrollpane = new JScrollPane();
frame = new JFrame();
frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
btn_save = new JButton("Save");
btn_save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
save();
}
});
btn_retrieve = new JButton("Retrieve");
btn_retrieve.setEnabled(false);
btn_retrieve.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
retrieve();
}
});
panel.add(btn_save);
panel.add(btn_retrieve);
frame.getContentPane().add(panel, BorderLayout.SOUTH);
createTree();
scrollpane.setViewportView(tree);
frame.getContentPane().add(scrollpane);
frame.setVisible(true);
frame.setSize(new Dimension(400,350));
}
private void createTree(){
DefaultMutableTreeNode node = new DefaultMutableTreeNode("root");
node.add(new DefaultMutableTreeNode("first"));
node.add(new DefaultMutableTreeNode("second"));
tree = new JTree(node);
}
public static void main(String str[]){
new TCJTree();
}
private void save(){
try{
XMLEncoder encoder = new XMLEncoder(new java.io.FileOutputStream
(filePath));
encoder.writeObject(scrollpane.getViewport().getView());
encoder.flush();
encoder.close();
scrollpane.setViewportView(null);
frame.invalidate();
frame.validate();
frame.repaint();
btn_retrieve.setEnabled(true);
btn_save.setEnabled(false);
}catch(Exception e){e.printStackTrace();}
}
private Object retrieve(){
Object obj = null;
try{
XMLDecoder decoder = new XMLDecoder(new java.io.FileInputStream
(filePath));
obj = decoder.readObject();
decoder.close();
tree = (JTree)obj;
scrollpane.setViewportView(tree);
frame.invalidate();
frame.validate();
frame.repaint();
btn_retrieve.setEnabled(false);
btn_save.setEnabled(true);
}catch(Exception e){e.printStackTrace();}
return obj;
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
constructor of DefaultTreeModel should be register in
java.beans.MetaData.java class, so that xml encoder can
make its new instance while serialization, and also
delegate for TreeModel should be provided in order to save
root node
(Review ID: 138903)
======================================================================