DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Pelle Guest

    Hide value in List


    How can I store a value and a hidden id (or something) for each row in a List
    (java.awt)? I want to be able to click the list and use the hidden value,
    pretty much like you use the itemData property on a listbox in Visual Basic.
    Or can this be done easily with another object ?


  2. #2
    Brent Worden Guest

    Re: Hide value in List

    javax.swing.JList is much better than java.awt.List for doing what you want.

    Any object, not just strings, can be added to a JList. The objects you add
    can house the id as well as the display text for each of your list items

    Simple example:
    public class MyListItem
    {
    private int id;
    private String text;

    public MyListItem(int id, String text)
    {
    super();
    this.id = id;
    this.text = text;
    }

    public int getId()
    {
    return id;
    }

    /**
    * displayed in list
    */
    public String toString()
    {
    return text;
    }
    }

    // creating the JList with MyListItem objects
    javax.swing.DefaultListModel model = new javax.swing.DefaultModel();
    model.add(new MyListItem(1, "Help"));
    model.add(new MyListItem(2, "Me"));

    javax.swing.JList list = new javax.swing.JList(model);

    // accessing the selected item's id
    MyListItem item = (MyListItem)list.getSelectedItem();
    int id = item.getId();

    HTH,

    Brent Worden
    http://www.Brent.Worden.org/

    "Pelle" <pehen@wmdata.com> wrote in message news:39ff00fb$1@news.devx.com...
    >
    > How can I store a value and a hidden id (or something) for each row in a

    List
    > (java.awt)? I want to be able to click the list and use the hidden value,
    > pretty much like you use the itemData property on a listbox in Visual

    Basic.
    > Or can this be done easily with another object ?
    >






Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links