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
– SUR1C4T3