Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JComboBox holding two values per entry?

Status
Not open for further replies.

milage

Programmer
Jul 13, 2001
58
US
Hi,

Is it possible to have a JComboBox which holds two values for each entry it lists such as a title and an underlying numeric value for that title? (As with select boxes in HTML)

Likewise, is it possible to do the same for a JList component?

Cheers

Rob
 
Alternatively you can use your own ADT as the combo box's value - an object that holds your numeric value and the title. example:

Code:
class OwnADT
{
  public int id;
  public String title;

  public OwnADT(id, title)
  {
    this.id = id;
    this.title = title;
  }

  public String toString()
  {
    return this.title;
  }
}

Like that you don't have to change the renderer and can access the numeric data via a direct cast:

Code:
int id = ((OwnADT)myComboBox.getSelectedItem()).id;
allow thyself to be the spark that lights the fire
haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top