Java:Swing JComboBox,是否可以为列表中的每个项目隐藏数据?
JComponents可以使用Java:Swing JComboBox,是否可以为列表中的每个项目隐藏数据?,java,swing,jcombobox,Java,Swing,Jcombobox,JComponents可以使用setName()和getName()获取隐藏数据,对吗?JComboBox项目呢?(我指的是JComboBox中的项,而不是JComboBox本身) 如果我有一个JComboBox,里面有一个用户名列表(例如),现在我希望列表中的每个用户名都有类似于“id”的东西,根据它们的排序方式,最好的方法是什么?创建一个User类,该类具有属性username和id;在.toString()中仅返回用户名,您的对象: import java.awt.*; import ja
setName()
和getName()
获取隐藏数据,对吗?JComboBox项目呢?(我指的是JComboBox中的项,而不是JComboBox本身)
如果我有一个JComboBox,里面有一个用户名列表(例如),现在我希望列表中的每个用户名都有类似于“id”的东西,根据它们的排序方式,最好的方法是什么?创建一个
User
类,该类具有属性username
和id
;在.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" ) );
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 );
}
}
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;
}
}
将项目添加到JComboBox:
JComboBox combo;
combo.addItem(new Item(1, "Test"));
combo.addItem(new Item(15,"Test 2"));
并获取项目:
Item selected_item = (Item) combo.getSelectedItem();
System.out.println(selected_item.getId());
System.out.println(selected_item.getName());
谢谢你的回答,真的解决了我的问题:)但是使用不推荐的代码“Vector”不是很糟糕吗?有没有办法避免使用向量?向量并不是不推荐使用的(至少在JDK6中是这样)。Swing组件使用模型来存储数据。DefaultComboxModel使用向量存储数据。如果你不喜欢这样,你可以创建你自己的模型,并使用你想要存储数据的任何东西。为了在JSomething中显示而重写toString不是推荐的方式。有关正确的方法,请参见@camickr的答案。为了在JSomething中显示而重写toString*不是推荐的方法。有关正确的方法,请参见@camickr的答案。这一点都不好(除了最琐碎的应用程序)-您最终会得到数十个项目变体,它们只在各自的toString实现中有所不同,只是因为您希望在不同的上下文中以不同的方式呈现它们。在Swing中,有一种机制被设计用来处理这种变化,这是一种渲染器。与预先做一些事情无关——你只是在Swing land中做错了;-)当然,你可以自由地相信其他的,不要去修复那些没有损坏的东西。琐碎问题-琐碎应用程序的琐碎答案。可能不是很有进取心。