Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • Last Modified:

Setting and retrieving hidden value from JComboBox

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 !
  • 2
1 Solution
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;
and by extension, to get the value of your hidden field

SecretEntry se = (SecretEntry)comboBox.getSelectedItem();
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now