Java 如何使用Map元素作为JComboBox的文本

Java 如何使用Map元素作为JComboBox的文本,java,swing,hashmap,jcombobox,Java,Swing,Hashmap,Jcombobox,我正在用集合的所有元素填充JComboBox(使用addItem())。集合中的每个元素都是一个HashMap(因此它是一个HashMap的组合框…) 我的问题是-假设我需要每个项目都是一个HashMap如何在GUI的组合框中将文本设置为apear?它需要是映射中某个键的值。通常,如果我用自己的类型填充一个组合框,我会跳过toString()方法……但我不知道如何实现,因为我使用的是Java HashMap 有什么想法吗(如果可能,不用实现我自己的HashMap) 更新:如果我想要自定义功能,似

我正在用集合的所有元素填充JComboBox(使用
addItem()
)。集合中的每个元素都是一个
HashMap
(因此它是一个HashMap的组合框…)

我的问题是-假设我需要每个项目都是一个
HashMap
如何在GUI的组合框中将文本设置为apear?它需要是映射中某个键的值。通常,如果我用自己的类型填充一个组合框,我会跳过
toString()
方法……但我不知道如何实现,因为我使用的是Java HashMap

有什么想法吗(如果可能,不用实现我自己的HashMap)


更新:如果我想要自定义功能,似乎无论如何都无法避免JComboBox中的对象位于toString()之上。我希望有一种方法(1)指定要加载到JComboBox中的对象,以及(2)指定这些对象在GUI中的显示方式。

如果要覆盖
toString()
method您可以创建一个decorator类,该类实现
Map
,并使用
HashMap
实现所需的方法,并给出自己的
toString()实现

如果您有一个hashmap,您将需要执行以下操作:

JComboBox box = new JComboBox(hashMap.getValues().toArray());
当然,您必须重写
HashMap

(2) 指定如何创建这些对象 显示在GUI中

您可以将任何对象添加到模型中,然后创建自定义渲染器,以任意方式显示对象。显示toString()方法和自定义渲染器方法的简单示例:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxItem extends JFrame implements ActionListener
{
    public ComboBoxItem()
    {
        Vector model = new Vector();
        model.addElement( new Item(1, "car" ) );
        model.addElement( new Item(2, "plane" ) );
        model.addElement( new Item(3, "train" ) );
        model.addElement( new Item(4, "boat" ) );

        JComboBox comboBox;

        //  Easiest approach is to just override toString() method
        //  of the Item class

        comboBox = new JComboBox( model );
        comboBox.setDragEnabled(true);
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.NORTH );

        //  Most flexible approach is to create a custom render
        //  to diplay the Item data

        comboBox = new JComboBox( model );
        comboBox.setDragEnabled(true);
        comboBox.setRenderer( new ItemRenderer() );
        comboBox.addActionListener( this );
        getContentPane().add(comboBox, BorderLayout.SOUTH );
    }

    public void actionPerformed(ActionEvent e)
    {
        JComboBox comboBox = (JComboBox)e.getSource();
        Item item = (Item)comboBox.getSelectedItem();
        System.out.println( item.getId() + " : " + item.getDescription() );
    }

    class ItemRenderer extends BasicComboBoxRenderer
    {
        public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus)
        {
            super.getListCellRendererComponent(list, value, index,
                isSelected, cellHasFocus);

            if (value != null)
            {
                Item item = (Item)value;
                setText( item.getDescription().toUpperCase() );
            }

            if (index == -1)
            {
                Item item = (Item)value;
                setText( "" + item.getId() );
            }


            return this;
        }
    }

    class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new ComboBoxItem();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible( true );
     }

}

是的,如果可能的话,我真的希望避免这种情况……但是谢谢你的建议,除非我误解了OP,否则情况并非如此。他有新的JComboBox(someCollection);而不仅仅是想要一个HashMap的值。