Populate java combo box with mySQL table value

Question: I have a Java NetBeans 6.9.1 project. In the desktop application when I am binding a combo box to mySQL database table and run the form, I get a combo box that is filled with root information of database row pks. This data presentation is useful for system administrators but not for my customers. I cannot select and see a value from my data entity class! I checked it several times. Data query and list work fine. The Name and Description fields are present but not shown. Can a combo box be used in a more elegant way? Answer: The brief answer is: yes. We have to customize how the combox cell is rendered. The values bound to the combo box are objects, not simple names. We must build our own display expressions for java swing combo boxes. NetBeans Binding library offers classes for binding JComboBox with data from a database. The IDE helps you generate all of this code. I looked at your code. You have already added the Entity Class (Address), the Query (queryAddress) and Query List (listAddress). NetBeans enables you to bind this custom Query List object to the component. Unfortunately it does not enable you to specify how to derive the values from the given (Address) database table. Your problems start here. You can find different solutions on Sun/Oracle web sites, but we give you a brief sample code here. 1. In the IDE’s GUI Builder right-clicking the combo box. 2. Select Customize Code and type the code below where the combo box is initialized. 3. Do not forget to adjust the sample code to your own entity class and field name.
// Address is your entity class (of mySQL table)
// a.getName() is the getter for the property in the entity class
// jComboBox1 is the name of the JComboBox instance
// Copy this to custom code area:
jComboBox1.setRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(
        JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (value instanceof Address) {         // ! Use your own entity class here
            Address a = (Address) value;
            setText(a.getName());               // ! Use your actual getter here
        }
        return this;
    }
});
The same renderer is used to render the drop down list and the selected item in the combo box. The selected value is rendered when the “index” is -1. You can also customize the toString method of your entity class (like Address class here) to show a unique text in the selected item box.
// ... see above
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Address) {  
    Address a = (Address) value;
    if( index == -1 ){      // The index param is not always valid, it is for demonstration only
        setText(a.getDescription());    // Selected item of the combo box
    }else{
        setText(a.getName());           // Drop down list items
        // setIcon(a.getIcon());         // with icons if you want
    }
}
return this;
A custom renderer may reside in its own class file (as a bean) and you can set the combo box’s renderer property to use it. The solution is here: Custom Java Desktop Database Application