如何使用Map元素作为JComboBox的文本 [英] How to use Map element as text of a JComboBox
问题描述
我将JComboBox(使用 addItem()
)填充到集合的所有元素。集合中的每个元素都是一个 HashMap
(所以它是一个哈希表的组合框..)。
我的问题是 - 鉴于我需要每个项目都是 HashMap
如何将文本设置为GUI中组合框的apear?它需要是地图中某个键的值。通常,如果我使用自己的类型填充组合框,那么我会覆盖 toString()
方法......但我不确定如何实现这一功能,因为我正在使用一个Java HashMap。
任何想法(如果可能,不实施我自己的HashMap)?
更新:如果我想要自定义功能,似乎没有办法避免让JComboBox中的对象覆盖toString()。我希望有一种方法(1)指定要加载到JComboBox中的对象,以及2)指定这些对象在GUI中的显示方式。
这些对象是
出现在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;
//最简单的方法是重写toString()方法
// Item类的
comboBox = new JComboBox(model);
comboBox.setDragEnabled(true);
comboBox.addActionListener(this);
getContentPane()。add(comboBox,BorderLayout.NORTH);
//最灵活的方法是创建一个自定义渲染
//以显示Item数据
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());
}
$ b $ 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());
}
返回此;
}
}
class Item
{
private int id;
私有字符串描述;
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;
$ b $ public static void main(String [] args)
{
JFrame frame = new ComboBoxItem();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
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..).
My question is - Given that I need each item to be a HashMap
how 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.
Any ideas (if possible, without implementing my own 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.
(2) specify how these objects are to appear in the 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:
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 );
}
}
这篇关于如何使用Map元素作为JComboBox的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!