Reputation: 15
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
Upvotes: 0
Views: 3926
Reputation: 1065
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.
Upvotes: 1
Reputation: 5871
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);
Upvotes: 1