serialising JLists

Discussion in 'Java' started by martin1976, Dec 19, 2006.

  1. martin1976

    martin1976 Guest

    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)
    martin1976, Dec 19, 2006
    #1
    1. Advertising

  2. On 19 Dec 2006 06:44:25 -0800, martin1976 wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Dec 19, 2006
    #2
    1. Advertising

  3. martin1976 wrote:

    > 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);
    >>>> 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();

    --
    Thomas
    Thomas Fritsch, Dec 19, 2006
    #3
  4. martin1976

    martin1976 Guest

    Sorry Silly mistake! Thanks for putting me straight
    martin1976, Dec 19, 2006
    #4
  5. martin1976 wrote:
    > 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.
    Andrew Thompson, Dec 19, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    serialising Arraylists

    Roedy Green, Sep 8, 2003, in forum: Java
    Replies:
    5
    Views:
    426
    Chris Uppal
    Sep 10, 2003
  2. freesoft_2000

    JLists

    freesoft_2000, Aug 25, 2005, in forum: Java
    Replies:
    2
    Views:
    1,655
    Roedy Green
    Aug 27, 2005
  3. Aaron Fude

    Mouse even on JLists

    Aaron Fude, Jul 27, 2008, in forum: Java
    Replies:
    6
    Views:
    802
    Martin Gregorie
    Jul 28, 2008
  4. Roedy Green

    Serialising Enums

    Roedy Green, Aug 20, 2009, in forum: Java
    Replies:
    11
    Views:
    564
    Roedy Green
    Aug 21, 2009
  5. Pete Hearn

    serialising parameters manually

    Pete Hearn, Jan 2, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    100
    Pete Hearn
    Jan 8, 2005
Loading...

Share This Page