Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By Parashurama

Thread: Storing objects in a Jlist

  1. #1
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Storing objects in a Jlist

    Hi.
    I have a form where i can enter values and add them to a Room object I have created. Now what I want is to have a JList populated by these Room objects so that when I select one item in the JList the data stored in that object is put in the form for editing. ('Im using NetBeans)

    My problem is that I can only add Strings to the JList, and cant really se how to "link" these strings to my actual objects.

    My list format looks like this "[roomnumber] Roomname", but this is just string parts I pull from the object, and don't really "connect" the item to anything.

    Does anyone have any pointers to get me in the right direction? As Im not very experienced with java it might be that JList is not the way to achieve my goal?

    Parashurama

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Storing objects in a Jlist

    Quote Originally Posted by Parashurama View Post
    My problem is that I can only add Strings to the JList
    And why is that?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Storing objects in a Jlist

    Excellent answer! Thanks for making me think.

    I just asumed as much since the examples I looked at just used strings. My bad...

    Ok, so I added the Room item directly, but I still struggle to have them show up the way I want. They just appear like this in the list : "Editors.Room@d5e270" while I want it like I mentioned in the first post.
    Do I set the "visible name" in the Jlist in the same addElement method?

    Im using DefaultListModel, but none of the methods in the link below seems to do what I need:

    DefaultListModel (Java Platform SE 7 )

    Got another pointer for me? :)

    Parashurama

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Storing objects in a Jlist

    Either override toString() or use a custom renderer.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Storing objects in a Jlist

    Ok, Ill look into the ListCellRenderer, thanks!

  6. #6
    Parashurama is offline Member
    Join Date
    Aug 2012
    Posts
    15
    Rep Power
    0

    Default Re: Storing objects in a Jlist

    Now I got the List to look the way I wanted, I just passed the string parts I wanted to my CellRendered class and added the string to a textfield which was placed in the list.

    Thanks again for your help DarryBruke!
    Fubarable likes this.

Similar Threads

  1. JAVA storing objects
    By santa in forum New To Java
    Replies: 24
    Last Post: 02-24-2012, 02:53 PM
  2. Replies: 2
    Last Post: 11-06-2011, 07:19 PM
  3. Storing objects in an array list?
    By Moedig in forum New To Java
    Replies: 3
    Last Post: 10-27-2011, 06:39 PM
  4. Storing objects
    By paul1024 in forum New To Java
    Replies: 5
    Last Post: 04-30-2011, 06:12 AM
  5. Storing objects directly with db4o
    By german in forum JDBC
    Replies: 0
    Last Post: 05-12-2009, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •