Java Swing 自定义 JComboBox 模型无法正确显示选项

Java Swing custom JComboBox Model can't display options correctly

我正在尝试创建一个名为 MyComboBoxModel 的自定义 JComboBox 模型。在模型中,项目列表是指定的。每个项目都是 class CompanyDropItem 的一个实例。我似乎可以用 5 个项目填充组合框,但它们显示不正确。下拉列表显示内存地址列表,而不是每个 CompanyDropItem 的 .companyName_ 值。此外,我不相信我已将其设置为正确 return 选择更改时每个 CompanyDropItem 的 .companyId_ 值。

代码只是一个游乐场,试图让这个概念发挥作用。我可以 post 创建下拉菜单的整个代码:

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;
        }
    }


}

感谢帮助

啊,是的,谢谢,这似乎有效。

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_;
        }
    }


}