I am specifying the files in the remote PC in a tree format, adding it to a panel and then sending it over the network. However whether I am sending JFrame
or JPanel
or FileSystemView
or JTree
I am getting the same error.
DefaultTreeModel
implements
Serializable
. It should be possible to serialize that across the network.
E.G. of serializing a tree model using XMLEncoder
.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.*;
import java.io.*;
import java.beans.XMLEncoder;
public class SerializeTreeModel {
private JComponent ui = null;
SerializeTreeModel() {
initUI();
}
public void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout());
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
JTree tree = new JTree();
tree.setVisibleRowCount(18);
DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
XMLEncoder xmlEncoder = new XMLEncoder(outStream);
xmlEncoder.writeObject(treeModel);
xmlEncoder.flush();
ui.add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new JScrollPane(tree),
new JScrollPane(new JTextArea(outStream.toString(), 2, 64))));
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
SerializeTreeModel o = new SerializeTreeModel();
JFrame f = new JFrame("Serialize a TreeModel");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
See also the File Browser GUI in which one of the major components is a tree.
