Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

serialising JLists

Thread view: 
martin1976 - 19 Dec 2006 14:44 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"
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.