4

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>
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
  • answer is use CombBoxModel, there you can everything starting with implemented Vector ends with Xml structures – mKorbel Mar 10 '12 at 22:33

3 Answers3

6

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;
        }
    }
 }
Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
  • -1 for suggesting to override toString for view reasons. The correct thingy to do is a custom renderer. – kleopatra Mar 11 '12 at 11:39
  • If I do this: `combo.addItem(customer.get(i).toString());`, I receive a list full of entries like `Customer@749395a`, `Customer@2864932e` etc. – Evgenij Reznik Mar 11 '12 at 15:53
  • 1
    @Evgeni Reznik You need to write the method toString() in the customer class first. Do you have a class Customer? Your custom toString() will return the proper name of the customer to be displayed in the combo box. You can then add the entire customer object into the combobox. – Vincent Ramdhanie Mar 11 '12 at 16:00
  • 1
    Evgeni Reznik I added a code examle to illustrate the point. After you do this and get the basic idea working you should pay attention to the comments by @kleopatra and mKorbel – Vincent Ramdhanie Mar 11 '12 at 16:53
3

Assuming you have a class Person that holds the names and ids, you can add instances of this class as objects of the combo box. getSelectedItem() will then return the instance of selected Person.

The problem is to display the person correctly in the combobox. You can either overload toString in the class to return the name, or you can supply your own ListCellRenderer to the combo box and render whatever you wish (for example, photo thumbnails) in the combo box.

Jakub Zaverka
  • 8,816
  • 3
  • 32
  • 48
0

I just answered another question at https://stackoverflow.com/a/10734784/11961 that explains a good way to create a custom ListCellRenderer that replaces toString() on the value class with alternative labels.

Community
  • 1
  • 1
MB.
  • 7,365
  • 6
  • 42
  • 42