Grab Id of an objects in the combobox

Asked

Viewed 699 times

2

In a Frame for registration of cars, I have a combobox1 with the brands of cars and another combobox2 with the models, when I choose a brand in CB1 only appear in CB2 the models related that brand. Brands and models are bank tables, which are used by a Cars table

 MARCAS
    idMarca
    nome

 MODELOS
    idModelo
    idMarca
    nome

CARROS
    idCarro
    placa
    idMarca
    idmodelo   

I can popular the two CB’s correctly searching the database and passing to Arraylist’s

public void preencherMarcas(){
   MarcasControle marcasControle = new MarcasControle();
   jComboBox1.removeAllItems();

    ArrayList<MarcasModelo> vetorMarcas = new ArrayList();  
    vetorMarcas = marcasControle.preencherMarcas();
    for (MarcasModelo marcas : vetorMarcas) {
         jComboBox1.addItem(marcas.getNome());
         }  
    }

    public void preencherModelos(){
    ModelosControle modelosControle = new ModelosControle();
    jComboBox2.removeAllItems();  
    ArrayList<ModelosModelo> vetorModelos = new ArrayList();  
    vetorModelos = modelosControle.preencherModelos(id);
        for (ModelosModelo modelos : vetorModelos) {
           jComboBox2.addItem(modelos.getNome());
        } 
     }   

However to persist the data I need to know the Idtag item selected in the combobox1 and the idModel of the selected combobox2 item. That’s my problem, I can’t close this reasoning on how to do it. How to get this information(idMarca and idModelo) from combombox’s. I tried it the way below, but it didn’t work, presented the error

Marcas mm = (Marcas)jComboBox1.getSelectedItem();
int idMarcas = m.getId();  

java.lang.String cannot be cast to template. Tags

  • If you are populating your combobox with strings, picking the selected one will also be a string. What you should do is, either popular the combobox with the objects themselves and keep the content of each item with the item description as you already have it now, or through the selected string, go to the object list and filter by this selected description

1 answer

2

The error occurs because you are populating the combobox with a set of strings and not with the type objects Marcas, as can be seen in your own code:

ArrayList<MarcasModelo> vetorMarcas = new ArrayList();  
vetorMarcas = marcasControle.preencherMarcas();
for (MarcasModelo marcas : vetorMarcas) {
     jComboBox1.addItem(marcas.getNome());
     }  
}

There is no way to cast String for your object, java does not know how to do this. Or you set Comb to only receive objects of type Marcas(which will probably even dismiss the cast) or change the way you are treating the selected item, such as a string and no cast and formulate another way to search for the id of the selected tag in the combobox.

If you choose to popular the combobox with your object, in the links below there are solutions that explain some ways to do this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.