简体   繁体   English

JComboBox设置selectedItem不起作用

[英]JComboBox set selectedItem not works

I'm building a swing application in Java. 我正在用Java构建一个swing应用程序。 I have a JComboBox that containts a list of Items. 我有一个JComboBox包含项目列表。 After that I wat to set selected Item of this JComboBox but I'm not able to do this. 之后,我想设置此JComboBox的选定项,但无法执行此操作。

This is the code that I use to add Item in JComboBox: 这是我用来在JComboBox中添加Item的代码:

List<Stagione> listaStagioni = modelManager.getStagioneManager().getAllStagioni(null, null);
ComboFormat comboStagione = new ComboFormat();
comboStagione.setModel(new javax.swing.DefaultComboBoxModel(listaStagioni.toArray()));
comboStagione.addItem("");
comboStagione.setSelectedIndex(listaStagioni.size());

This is the class Stagione: 这是Stagione类别:

public class Stagione {
    private Integer id;
    private Integer anno;
    private String descrizione;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getAnno() {
        return anno;
    }
    public void setAnno(Integer anno) {
        this.anno = anno;
    }
    public String getDescrizione() {
        return descrizione;
    }
    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String toString() {
            return this.getDescrizione();
    }
}

In this code, I want to set from code an Item in JComboBox 在这段代码中,我想从代码中设置JComboBox中的Item

comboCategoria.setSelectedItem("MAGLIE");

I don't have any error but the item is not selected. 我没有任何错误,但未选中该项目。 This is the item in my JComboBox 这是我的JComboBox中的项目 在此处输入图片说明

setSelectedItem internaly uses the equals method. setSelectedItem内部使用equals方法。 You add Stagione objects, but use a String within setSelectItem. 您添加了Stagione对象,但在setSelectItem中使用了String。

The easiest solution in your case is to override equals in the Stagion object to handle String comparison. 在这种情况下,最简单的解决方案是重写Stagion对象中的equals以处理String比较。

Like : 喜欢 :

 @Override
public boolean equals(Object obj) {
    if(obj==null){
        return false;
    }

    if(obj instanceof Stagione){
        return ((Stagione)obj).getId().equals(getId());
    }else if (obj instanceof String){
        return descrizione.equals(obj);
    }else {
        // Or return false...
        return super.equals(obj);
    }
}

PS : I'm not a big fan of instanceof use. PS:我不喜欢使用instanceof。 Comaring String and Business Objects, well... Using Stagione object in setSelectedItem would be cleaner from an object point of view, but you'd have to implements equals anyway, to compare by IDs for example. 从字符串的角度来看,在字符串和业务对象之间进行编排很好...从对象的角度来看,在setSelectedItem中使用Stagione对象会更干净,但是无论如何,您都必须实现等于,例如要通过ID比较。

Another common way around this it to set the index instead of the item. 解决此问题的另一种常见方法是设置索引而不是项目。 Something like this; 像这样的东西;

String testValue = "MAGLIE";

for (int i=0; i<combobox.getModel().getSize(); i++)
{
    if (combobox.getItemAt(i).toString().equals(testValue))
    {
        combobox.setSelectedIndex(i);
        break;
    }
}

Not directly related to your question but the following won't work: 与您的问题没有直接关系,但以下操作无效:

comboStagione.setSelectedIndex(listaStagioni.size());

Java indexes are 0 offset, so to select the last item is a list you use: Java索引的偏移量为0,因此要选择的最后一项是您使用的列表:

comboStagione.setSelectedIndex(listaStagioni.size() - 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM