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);
>>> objectIn.readObject(jList1); <<< this line wont compile
}//... .. .. 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)
Gordon Beaton - 19 Dec 2006 14:55 GMT
> 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

Signature
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Thomas Fritsch - 19 Dec 2006 15:32 GMT
> 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
>
[quoted text clipped - 5 lines]
> ObjectInputStream objectIn = new ObjectInputStream(fileIn);
>>>> objectIn.readObject(jList1); <<< this line wont compile
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();

Signature
Thomas
martin1976 - 19 Dec 2006 16:10 GMT
Sorry Silly mistake! Thanks for putting me straight
Andrew Thompson - 19 Dec 2006 16:18 GMT
> 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.