Hide value in List


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Hide value in List

  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 ?
    >






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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center