java 如何使用 Map 元素作为 JComboBox 的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2812850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Map element as text of a JComboBox
提问by llm
I am populating a JComboBox (using addItem()) with all the elements of a collection. Each element in the collection is a HashMap(so its a ComboBox of Hashmaps..).
我正在用addItem()集合的所有元素填充 JComboBox(使用)。集合中的每个元素都是一个HashMap(所以它是一个Hashmaps 的 ComboBox ......)。
My question is - Given that I need each item to be a HashMaphow do I set the text to apear in the combobox on the GUI? It needs to be the value of a certain key in the map. Normally if I am populating a combobox with my own type, I would just overide the toString()method...but I am not sure how to acheive this since I am using a Java HashMap.
我的问题是 - 鉴于我需要每个项目都是一个HashMap如何将文本设置为出现在 GUI 的组合框中?它需要是地图中某个键的值。通常,如果我使用自己的类型填充组合框,我只会覆盖该toString()方法......但我不确定如何实现这一点,因为我使用的是 Java HashMap。
Any ideas (if possible, without implementing my own HashMap)?
任何想法(如果可能,不实施我自己的 HashMap)?
Update: It seems like there isn't anyway to avoid having the object int the JComboBox overide toString() if I want custom functionality..I wish there was a way to (1) specify the objects to be loaded into the JComboBox and (2) specify how these objects are to appear in the GUI.
更新:如果我想要自定义功能,似乎没有办法避免将对象嵌入 JComboBox 覆盖 toString() ..我希望有一种方法可以 (1) 指定要加载到 JComboBox 中的对象和 ( 2) 指定这些对象在 GUI 中的显示方式。
回答by camickr
(2) specify how these objects are to appear in the GUI.
(2) 指定这些对象在 GUI 中的显示方式。
You can add any Object to the model and then create a custom renderer to display the object any way you want. Simple example that shows the toString() approach and custom renderer approach:
您可以将任何对象添加到模型,然后创建自定义渲染器以任何您想要的方式显示对象。显示 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 );
}
}
回答by Andrew Hubbs
If you want to override the toString()method you could just create a decorator class that implements Mapand uses a HashMapto implemented the needed methods and give your own implementation of toString().
如果您想覆盖该toString()方法,您只需创建一个装饰器类,该类实现Map并使用 aHashMap来实现所需的方法,并提供您自己的toString().
回答by Cristian
If you have a hashmap, you will want to do something like:
如果你有一个哈希图,你会想要做这样的事情:
JComboBox box = new JComboBox(hashMap.getValues().toArray());
Of course, you have to override the toStringmethod of the object you have in the HashMap
当然,您必须覆盖toString您在HashMap

