Adding a class instance to JComboBox [duplicate]
Asked Answered
K

1

0

In this question (Adding items to a JComboBox) it is described how an Item can be added to a JComboBox. This would allow to store an Object in a JComboBox, and overriding the toString() method the JComboBox would display a value and it will return the whole object.

I have included the following class:

public class ComboItem {

    private String key;
    private String value;

    public ComboItem(String key, String value)
    {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString()
    {
        return key;
    }

    public String getKey()
    {
        return key;
    }

    public String getValue()
    {
        return value;
    }
}

And then I try to add this to my JComboBox comboTimeZoneChart:

comboTimeZoneChart.addItem(new ComboItem("bar", "foo"));

But I get the following error in Netbeans:

incompatible types: ComboItem cannot be converted to String

I have double check but I do not know what could be wrong. This issue/problem is also reflected with 5 upvotes in the accepted answer of the original question included so it seems I am not the only one facing this issue.

I include an image of the error:

enter image description here

Ken answered 10/11, 2019 at 14:13 Comment(7)
Please include a minimal reproducible example.Kostroma
Are you sure it isn't something as simple as using a wrong generic, i.e., using a JComboBox<String> when you should be using JComboBox<ComboItem>?Rafaelof
In fact, this has to be the problem -- an incorrect generic type for the combo box, its model or both.Rafaelof
And I'm afraid that you're not posting the most important piece of code in your question -- where you declare your combo box and its modelRafaelof
Thanks for pointing the duplicate and isolating the issue. The problem was located exactly in the post marked to have the same question/answer. No matter how simple it might appear, I did not know that you could define JComboBox<ComboItem>. Additionally, as I was using Netbeans UI builder I never saw that part of the code JComboBox<String>. I have replaced JComboBox <String> with JComboBox<ComboItem> and the problem is gone.Ken
As a side note to change that in Netbeans IDE UI, you just right click on the JComboBox item, click Properties, go to Code tab and specify the right type -ComboItem in this case- in the Type Parameters field.Ken
As a further side note, you might need to further edit code generated by Netbeans IDE if you change the JComboBox TypeParameters after you have created it with the default String. To do that you need to right click and click on Customize Code. It is a bit cumbersome to change it. I needed a couple of iterations with the IDE. I am mentioning as I guess most people will be using the UI builder.Ken
J
-2

comboTimeZoneChart.addItem(new ComboItem("bar","foo").toString());

Jody answered 10/11, 2019 at 14:21 Comment(1)
This is bad advice since the whole reason that the OP is creating the ComboItem entry class is to save information held in the combo box model in a useable form. Your suggestion discards all this information and for no benefit.Rafaelof

© 2022 - 2024 — McMap. All rights reserved.