Problem with setSelectedItem in JComboBox

0

I have a problem fixing an object in a combobox. I explain the situation. I have a mysql a table with a data series (client table), one of those data is a foreign key to another table (city table). In my java program I create an object with the customer's data, fill a comboBox with the list of clients, when you choose one of these clients of the combo, the data is recovered. The city also recovers, once recovered I want that city to be selected in the combo with the list of all the city there is.

That is, if client 1 has city 2, when client 1 is recovered in the city combo, city 2 must appear. But here is the problem that remains empty. I put the code:

CiudadVO ciudad = new CiudadVO();
CiudadDAO ciudaddao = new CiudadDAO();
ciudad = ciudaddao.buscarCiudad(clienteModificado.getIdCiudad());
comboCiudad.setSelectedItem(ciudad); 

I create a city object with the id city recovered from the database, this does it well and I tried to print it and that's right, I create a city object from that id, now I want to put it in the combo with setSelectedItem and it does not work. I do not know what the problem is, if anyone knows it would be very helpful.

Thanks

    
asked by Ariishiia 22.04.2017 в 20:25
source

1 answer

0

The setSelectedItem () method uses the equals () method to determine which one to select. Therefore, you should have defined this method in the CiudadVO class. I imagine that may be your problem.

For example, in the VOICE class the equals method must be defined, leaving something like this:

@Override
public boolean equals(Object other) {
    return this.id == ((CiudadVO) other).id;
}
    
answered by 23.04.2017 в 02:06