10

我正在使用addItem()集合的所有元素填充 JComboBox(使用)。集合中的每个元素都是一个HashMap(所以它是一个 Hashmaps 的 ComboBox ..)。

我的问题是 - 鉴于我需要每个项目都是一个HashMap我如何设置文本出现在 GUI 的组合框中?它需要是映射中某个键的值。通常,如果我用自己的类型填充组合框,我会覆盖该toString()方法......但我不确定如何实现这一点,因为我使用的是 Java HashMap。

任何想法(如果可能,不实现我自己的 HashMap)?

更新:如果我想要自定义功能,似乎没有办法避免让 JComboBox 中的对象覆盖 toString() ......我希望有一种方法可以(1)指定要加载到 JComboBox 中的对象和( 2) 指定这些对象在 GUI 中的显示方式。

4

3 回答 3

11

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

}
于 2010-05-11T17:49:23.400 回答
0

如果您想覆盖该toString()方法,您可以创建一个装饰器类,该类实现Map并使用 aHashMap来实现所需的方法并提供您自己的toString().

于 2010-05-11T17:16:39.757 回答
0

如果你有一个 hashmap,你会想要做类似的事情:

JComboBox box = new JComboBox(hashMap.getValues().toArray());

当然,你必须重写toString你在HashMap

于 2010-05-11T17:18:12.670 回答