JComboBox项目列表 [英] JComboBox Item List
问题描述
我想知道如何在JComboBox中更改项目列表的名称?这是我的代码
我想将其更改为Dog,Panda,bee。而不是选择他们的路径。
I am wondering how would I change the name of the item list in my JComboBox?here's my code I want to change it to Dog, Panda, bee. rather than choosing their path.
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
public class ComboTest {
private JLabel imageLabel;
private JComboBox comboImage;
private String[] names = {"images/dog.gif","images/bee.gif","images/Panda.gif"};
private Icon[] icons = {
new ImageIcon(getClass().getResource(names[0])),
new ImageIcon(getClass().getResource(names[1])),
new ImageIcon(getClass().getResource(names[2])),
};
public ComboTest(){
initComponents();
}
public void initComponents(){
JFrame frame = new JFrame("Test Combo");
frame.setVisible(true);
frame.setSize(320, 160);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
comboImage = new JComboBox(names);
comboImage.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange() == ItemEvent.SELECTED){
imageLabel.setIcon(icons[comboImage.getSelectedIndex()]);
}
}
});
frame.add(comboImage);
imageLabel = new JLabel(icons[0]);
frame.add(imageLabel);
}
}
推荐答案
你可能想要创建一个具有两个属性的对象,即要显示的路径和文本。
You probably want to make an object with two properties, the path and the text you want to display.
然后你将设置 toString
返回text属性的方法。免责声明:我尚未测试任何此代码。
Then you'll set the toString
method to return the text property. Disclaimer: I haven't tested any of this code.
public class ValueText {
private String text;
private String value;
public ValueText(final String text, final String value) {
this.text = text;
this.value = value;
}
@Override
public String toString() {
return text;
}
public String getValue() {
return value;
}
}
然后你可以将你的初始数组更改为:
Then you can change your initial array to something like:
private Object[] names = {
new ValueText("Dog", "images/dog.gif"),
new ValueText("Bee", "images/bee.gif"),
new ValueText("Panda", "images/Panda.gif")
};
它应该类似,只是现在,当您检查所选项目时,您可以使用 getValue()
获取路径的方法。
And it should work similarly, only now, when you are inspecting the selected item, you can use the getValue()
method to get the path.
您可能也对自定义渲染器感兴趣,但它可能是没有必要为您的使用:
的http://文档.oracle.com / javase / tutorial / uiswing / components / combobox.html #renderer
You also might be interested in a custom renderer, but it's probably not necessary for your use: http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer
更新我会继续在kleopatra在评论中提出了一些令人信服的论据之后做了更正,你应该在下面阅读。
Update I'll go ahead and make a correction after kleopatra made some convincing arguments in the comments, which you should read below.
一个更通用,更清晰的方法是使用自定义渲染器,即使它非常简单(见上面的链接)。
A more general purpose and cleaner way of doing this is with a custom renderer, even if it's very simple (see link above).
这篇关于JComboBox项目列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!