-1

I am trying to send a JPanel object to the server1. While doing so I get the error -

java.io.NotSerializableException: javax.swing.filechooser.WindowsFileSystemView

This is the code I have written.

private JPanel gui;
public URL url = null;
public URLConnection conn = null;
ObjectOutputStream writer;

url = new URL(EndUserFrame.uri);
conn = url.openConnection();
conn.setDoOutput(true);
writer = new ObjectOutputStream(new BufferedOutputStream(
conn.getOutputStream()));
writer.writeObject(gui);

How to fix this error?

  1. 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.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
raghavanm
  • 13
  • 6
  • To write object in OutputStream the Object must be serializable. I your case JPanel is not serializable. – Kanti Mar 09 '15 at 11:14
  • 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. – raghavanm Mar 09 '15 at 11:19
  • To read or write object from/to ObjectInputStream/ObjectOutputStream, the object must be serializable... – Kanti Mar 09 '15 at 11:23
  • *"I am specifying the files in the remote PC in a tree format."* [`DefaultTreeModel`](https://docs.oracle.com/javase/8/docs/api/javax/swing/tree/DefaultTreeModel.html) **`implements`** [`Serializable`](https://docs.oracle.com/javase/8/docs/api/java/io/Serializable.html) .. – Andrew Thompson Mar 09 '15 at 12:30

2 Answers2

1

How to fix this error?

There is no fix. You cannot do that. Many Swing and AWT classes are simply not serializable.

If you really need to send Swing screen layouts (or something like that) you will need to extract the information that you are trying to send into a different (serializable) data structure.

On the other hand, some Swing classes are serializable; see @Andrew Thompson's answer

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

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.

Serialize a TreeModel

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.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433