0

I have a JComboBox and a class named clsPais:

public class clsPais {

private long id = 0;
private String nombre = "";

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getNombre() {
        return nombre;
    }
    public void setNombre(String nombre) {
        this.nombre = nombre;
    }
@Override
    public String toString() {
        return nombre;
    }

}

In my JFrame code I put:

clsPais p1 = new clsPais();
p1.setId(1);
p1.setNombre("ARGENTINA");

clsPais p2 = new clsPais();
p2.setId(2);
p2.setNombre("BRASIL");

cmbPaises.removeAllItems();
cmbPaises.addItem(p1);

Here, i have an error, telling me "incompatible types: clsPais canot be converted to string". The addItem from my JComboBox only accept String parameter. What can I do?

Thank you

jorge_barmat
  • 15
  • 1
  • 4
  • Possible duplicate of [Adding items to a JComboBox](http://stackoverflow.com/questions/17887927/adding-items-to-a-jcombobox) – tavi Dec 05 '16 at 13:25
  • First of all, you may value everyone else time, and to edit your question. "What can I do?" - is not a valid question. Thanks for the understanding. – Farside Dec 05 '16 at 13:41
  • How can I value the answers? I'm new reporting here. Excuse me and Thanks. – jorge_barmat Dec 07 '16 at 10:19

2 Answers2

1

you can do it as follows..

    JComboBox<ClsPais> comboBox = new JComboBox<>();

    clsPais p1 = new clsPais();
    p1.setId(1);
    p1.setNombre("ARGENTINA");

    clsPais p2 = new clsPais();
    p2.setId(2);
    p2.setNombre("BRASIL");


    comboBox.addItem(p1);
    comboBox.addItem(p2);
Jobin
  • 5,610
  • 5
  • 38
  • 53
1

You need cmbPaises to be of type clsPais rather than String:

JComboBox<clsPais> cmbPaises = new JComboBox<>();
cmbPaises.addItem(p1);
cmbPaises.addItem(p2);

BTW, in Java, the convention is that class names begin with caplital letters.

unlimitednzt
  • 1,035
  • 2
  • 16
  • 25
  • With NetBeans, i had problems to redefine JComboBox type. I edit the .java with Notepadd++ and then i could change the type. Before that, it works, for every JComboBox that i need. Thanks you again. – jorge_barmat Dec 07 '16 at 10:16