Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:46:14+00:00 2026-05-31T04:46:14+00:00

Is it possible, to define values different from the actual content in a JComboBox?

  • 0

Is it possible, to define values different from the actual content in a JComboBox?
In HTML it looks as follows:

<select>
  <option value="value1">Content1</option>
  <option value="value2">Content2</option>
  <option value="value3">Content3</option>
</select>

Here it’s possible to get a short value, no matter how long its content is.

In Java I only know the following solution:

// Creating new JComboBox with predefined values
   String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" }; 
   private JComboBox combo = new JComboBox(petStrings);

// Retrieving selected value
   System.out.println(combo.getSelectedItem());

But here I only would get “Cat”, “Dog”, etc.

The problem is, that I want to load all names of the customers from a database into the JComboBox and then retrieve the ID from the selected customer. It should look like this:

<select>
  <option value="104">Peter Smith</option>
  <option value="121">Jake Moore</option>
  <option value="143">Adam Leonard</option>
</select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:46:16+00:00Added an answer on May 31, 2026 at 4:46 am

    If you create a Customer class and load a list of Customer objects into the combobox then you will get what you want. The combo box will display the toString() of your object, so the Customer class should return the name in toString(). When you retrieve the selected item its a Customer object from which you can get the id or whatever else you want.


    Here is an example to illustrate what I am suggesting. However, it would be a good idea to follow kleopatra’s and mKorbel’s advice when you get this basic idea working.

    import java.awt.BorderLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class ComboJumbo extends JFrame{
    
        JLabel label;
        JComboBox combo;
    
        public static void main(String args[]){
            new ComboJumbo();
        }
    
        public ComboJumbo(){
            super("Combo Jumbo");
            label = new JLabel("Select a Customer");
            add(label, BorderLayout.NORTH);
    
            Customer customers[] = new Customer[6];
            customers[0] = new Customer("Frank", 1);
            customers[1] = new Customer("Sue", 6);
            customers[2] = new Customer("Joe", 2);
            customers[3] = new Customer("Fenton", 3);
            customers[4] = new Customer("Bess", 4);
            customers[5] = new Customer("Nancy", 5);
    
            combo = new JComboBox(customers);
            combo.addItemListener(new ItemListener(){
    
                public void itemStateChanged(ItemEvent e) {
                    Customer c = (Customer)e.getItem();
                    label.setText("You selected customer id: " + c.getId());
                }
    
            });
            JPanel panel = new JPanel();
            panel.add(combo);
            add(panel,BorderLayout.CENTER);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 200);
            setVisible(true);
        }
    
    
        class Customer{
            private String name;
            private int id;
    
            public Customer(String name, int id){
                this.name = name;
                this.id = id;
            }
    
            public String toString(){
                return getName();
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public int getId() {
                return id;
            }
    
            public void setId(int id) {
                this.id = id;
            }
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to define an instance method in ruby from a string (the
For instance, is the following possible: #define definer(x) #define #x?
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
Is it possible to define a timestamp column in a MySQL table that will
Is it possible to define a class in C# such that class GenericCollection<T> :
In VBP its possible to define user defined actions by creating a custom COM
Is it possible to define a spring-managed EJB3 hibernate listener? I have this definition
Is it possible to define anonymous subroutines in a hash constructor in Perl? I'm
Is it possible to define two variables in a vb2005 For loop in a
Is it possible to define a regex which will match every character except a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.