serialising JLists

M

martin1976

I'm trying to store a JList in a file. I got some code off the web for
loading one and that copiles, so I just changed all the outputs to
inputs and all the writes to reads in an attempt to read it back again.
Unfortunately the line objectIn.readObject(jList1) wont compile it says

"readObject in java.io_ObjectInputStream cannot be applied to
java.swing.JList"
JList implements Serialisable so I can't see what the problem is. Does
anybody know of another way of storing a jlist using serialization



public void readObjectFromFile(String FileName)
{
System.out.println("Serialising From Disk\n");
try
{
InputStream fileIn = new FileInputStream("test.txt");
try
{
ObjectInputStream objectIn = new ObjectInputStream(fileIn); }//... .. .. try
catch (Exception e)
{
System.out.println("exception occurred\n");
}//... .. .. catch (Exception e)
finally
{
fileIn.close();
}//... .. .. finally
}//... .. .. try
catch (IOException e)
{
e.printStackTrace();
}//... .. .. catch (IOException exc)
}//... .. .. public void readObjectFromFile(String FileName)
 
G

Gordon Beaton

I'm trying to store a JList in a file. I got some code off the web
for loading one and that copiles, so I just changed all the outputs
to inputs and all the writes to reads in an attempt to read it back
again. Unfortunately the line objectIn.readObject(jList1) wont
compile it says "readObject in java.io_ObjectInputStream cannot be
applied to java.swing.JList"

It's not a problem related to Serialization. Writing a value and
reading one are fundamentally different things.

Consider:

output.printLine(someString);

but:

someString = input.readLine();

So:

jList1 = (JList)objectIn.readObject();

/gordon
 
T

Thomas Fritsch

martin1976 said:
I'm trying to store a JList in a file. I got some code off the web for
loading one and that copiles, so I just changed all the outputs to
inputs and all the writes to reads in an attempt to read it back again.
ObjectInputStream and ObjectOutputStream are not as parallel as you thought.
Better than guessing the API is reading the API doc. See at
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectInputStream.html
http://java.sun.com/j2se/1.4.2/docs/api/java/io/ObjectOutputStream.html
ObjectOutputStream has:
public void writeObject(Object obj) throws ...
ObjectInputStream has:
public Object readObject() throws ...
Unfortunately the line objectIn.readObject(jList1) wont compile it says

"readObject in java.io_ObjectInputStream cannot be applied to
java.swing.JList"
JList implements Serialisable so I can't see what the problem is. Does
anybody know of another way of storing a jlist using serialization

ObjectInputStream objectIn = new ObjectInputStream(fileIn);
The problem is that ObjectInputStream has no method readObject(Object).
When you have
objectOut.writeObject(jList1);
on the writing side, its counterpart on the reading side would be:
JList jList1 = (JList) objectIn.readObject();
 
A

Andrew Thompson

martin1976 said:
I'm trying to store a JList in a file. ..

Out of curiousity. Why?

( and can the app.? when deployed to users?
withstand the serialization breaking each time
the user upgrades their Java? )

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top