Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Setting and retrieving hidden value from JComboBox

Posted on 2003-03-14
Medium Priority
Last Modified: 2008-03-17
hey there,

i was wondering, can I set a hidden value on an item in a JComboBox (like an ID or so) so that when an item is selected, the hidden value is returned instead of the item itself.


i get ID, name and first name out of a DB

in a loop, i do a box.additem("name" + " " + first_name);
but i want to set the corresponding ID with that item, somewhere hidden...

like in html it would be <option value="id">name + first_name</option>

Can anyone help, thank you !
Question by:msouphy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 86

Accepted Solution

CEHJ earned 300 total points
ID: 8135298
An item in a JComboBox is an Object. What you *see* in the combo box is only partially connected to what is *in* it, and what you see is dertermined by toString of the object in question. The relationship is similar to model-view. All you need to do is this:

public class SecretEntry {
  String visibleField;
  String hiddenField;
  // other methods
  public String toString() {
    return visibleField;
LVL 86

Expert Comment

ID: 8135308
and by extension, to get the value of your hidden field

SecretEntry se = (SecretEntry)comboBox.getSelectedItem();

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question