Java Swing custom JComboBox Model can’t display options correctly

Solution for Java Swing custom JComboBox Model can’t display options correctly
is Given Below:

I’m trying to create a custom JComboBox model called MyComboBoxModel. In the model the list of items is specified. Each item is an instance of class CompanyDropItem. I seem to be able to fill the combobox with 5 items, but they arent displaying correctly. The dropdown shows a list of memory addresses instead of the .companyName_ value for each CompanyDropItem. Also, I don’t believe I have it set up to correctly return the .companyId_ value of each CompanyDropItem when the selection changes.

The code is just a playground trying to get the concept to work. i can post the entire code that creates the dropdown:

package Package;

import java.awt.Color;
import java.awt.Panel;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    static MyComboBoxModel model;


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame();
        frame.setSize(200, 200);

        JPanel panel = new JPanel();
        panel.setBackground(Color.red);
        frame.add(panel);

        model = new MyComboBoxModel();

        CompanyDropItem[] items = new CompanyDropItem[5];
        items[0] = new CompanyDropItem(1, "A");
        items[1] = new CompanyDropItem(2, "B");
        items[2] = new CompanyDropItem(3, "C");
        items[3] = new CompanyDropItem(4, "D");
        items[4] = new CompanyDropItem(5, "E");
        model.companyDropItems = items;

        JComboBox cbox = new JComboBox(model);
        cbox.setMaximumRowCount(5);
        frame.add(cbox);

        frame.setVisible(true);
    }
    
    
    static class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {

        public CompanyDropItem[] companyDropItems;

        String selection = null;

        public Object getElementAt(int index) {
            return companyDropItems[index];
        }

        public int getSize() {
            return companyDropItems.length;
        }

        public void setSelectedItem(Object anItem) {
            // ?
        }

        public Object getSelectedItem() {
            return selection;
        }
    }

    private static class CompanyDropItem {
        final int companyId_;
        final String companyName_;

        private CompanyDropItem(int companyId, String companyName) {
            companyId_ = companyId;
            companyName_ = companyName;
        }
    }


}

Thanks for the help

Ah yes thank you, this seems to work.

package Package;

import java.awt.Color;
import java.awt.Panel;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    static MyComboBoxModel model;


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JFrame frame = new JFrame();
        frame.setSize(200, 200);

        JPanel panel = new JPanel();
        panel.setBackground(Color.red);
        frame.add(panel);

        model = new MyComboBoxModel();

        CompanyDropItem[] items = new CompanyDropItem[5];
        items[0] = new CompanyDropItem(1, "A");
        items[1] = new CompanyDropItem(2, "B");
        items[2] = new CompanyDropItem(3, "C");
        items[3] = new CompanyDropItem(4, "D");
        items[4] = new CompanyDropItem(5, "E");
        model.companyDropItems = items;

        JComboBox cbox = new JComboBox(model);
        cbox.setMaximumRowCount(5);
        frame.add(cbox);

        frame.setVisible(true);
    }
    
    
    static class MyComboBoxModel extends AbstractListModel implements ComboBoxModel {

        public CompanyDropItem[] companyDropItems;

        CompanyDropItem selection = null;

        public Object getElementAt(int index) {
            return companyDropItems[index];
        }

        public int getSize() {
            return companyDropItems.length;
        }

        public void setSelectedItem(Object anItem) {
            selection = (CompanyDropItem)anItem;
        }

        public Object getSelectedItem() {
            return selection;
        }
    }

    private static class CompanyDropItem {
        final int companyId_;
        final String companyName_;

        private CompanyDropItem(int companyId, String companyName) {
            companyId_ = companyId;
            companyName_ = companyName;
        }
        public String toString() {
            return companyName_;
        }
    }


}