Question

I'm trying to open/save a Text Object from SWT with the ObjectOutputStream. But it doesn't work. Have anybody an idea, why?

public static void read(String fileName, Text textField) {
    int c=0;
    try {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));

        c= in.readInt();

        textField = (Text) in.readObject();

        in.close();
    } 
    catch(IOException e){
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static void write(String fileName, Text textField) {
    int c = 1;
    try {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
        out.writeInt(c);
        out.writeObject((Text)textField);

        out.close();

    } 
    catch(IOException e){
        e.printStackTrace();
    }
}

The Error which appears on the console when i save:

java.io.NotSerializableException: org.eclipse.swt.widgets.Text
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at FileIO.write(FileIO.java:42)
    at SelectionAdapterSave.widgetSelected(SelectionAdapterSave.java:29)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
    at Editor.open(Editor.java:230)
    at EditorMain.main(EditorMain.java:6)

and this when i try to open the file, what i saved before:

Caused by: java.io.NotSerializableException: org.eclipse.swt.widgets.Text
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at FileIO.write(FileIO.java:42)
    at SelectionAdapterSave.widgetSelected(SelectionAdapterSave.java:29)
    ... 7 more
Was it helpful?

Solution

You can only use ObjectOutputStream on objects which implement Serializable. The SWT Text class does not implement this.

A SWT Text class contains all sorts of objects which depend on the native UI code, there is no way this can be saved and loaded again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top