Java:Swing JComboBox,是否可以为列表中的每个项目隐藏数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5010537/
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
Java: Swing JComboBox, is it possible to have hidden data for each item in the list?
提问by evilReiko
JComponents can obtain hidden data using setName()
and getName()
, right? What about JComboBox items? (I'm referring to the itemsin the JComboBox, NOT the JComboBox itself)
JComponents 可以使用setName()
and获取隐藏数据getName()
,对吗?JComboBox 项目呢?(我指的是 JComboBox 中的项目,而不是 JComboBox 本身)
What if I have a JComboBox, and inside it I have a list of usernames (for example), now I want to have something like 'id' for each username in the list according to how they are ordered, what's the best way to do this?
如果我有一个 JComboBox,并且在其中我有一个用户名列表(例如),现在我想根据它们的排序方式为列表中的每个用户名设置类似“id”的东西,最好的方法是什么这?
回答by camickr
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" ) );
model.addElement( new Item(5, "boat aadf asfsdf a asd asd" ) );
JComboBox comboBox;
// Easiest approach is to just override toString() method
// of the Item class
comboBox = new JComboBox( model );
comboBox.addActionListener( this );
comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
getContentPane().add(comboBox, BorderLayout.NORTH );
// Most flexible approach is to create a custom render
// to diplay the Item data
comboBox = new JComboBox( model );
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 Akna
Your object :
你的对象:
public class Item {
private int id;
private String name;
public Item(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return this.name;
}
}
Add items to your JComboBox :
将项目添加到您的 JComboBox :
JComboBox combo;
combo.addItem(new Item(1, "Test"));
combo.addItem(new Item(15,"Test 2"));
And get Item :
并获得项目:
Item selected_item = (Item) combo.getSelectedItem();
System.out.println(selected_item.getId());
System.out.println(selected_item.getName());
回答by atamanroman
Create a User
class which has the attributes username
and id
; return only username
in .toString()
.
创建一个User
具有属性username
和的类id
;仅username
在.toString()
.