Estou com dúvidas quanto ao JComboBox java swing
Tenho a seguinte função dentro da view Empresa
public void carregaCombobox()
{
this.cbbUf.setModel(ctruf.estados());
this.cbbCidade.setModel(ctrcid.cidades(this.cbbUf.getSelectedItem().toString()));
cbbUf.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
Object item = e.getItem();
cbbCidade.setModel(ctrcid.cidades(item.toString()));
}
}
});
}
onde
em uma Controladora e depois em uma DAO
tenho DAO:
public DefaultComboBoxModel estados()
{
DefaultComboBoxModel listmodel = new DefaultComboBoxModel();
String query = "select * from uf";
ResultSet rs = Banco.getCon().consultar(query);
try
{
while (rs.next())
{
listmodel.addElement(rs.getString("uf_sigla"));
}
rs.close();
} catch (SQLException ex)
{
System.err.println("Erro consulta :" + ex.getMessage());
}
return listmodel;
}
public DefaultComboBoxModel cidades(String filtro)
{
DefaultComboBoxModel listmodel = new DefaultComboBoxModel();
String query = "select cid_nome from cidade as c inner join uf as u on c.uf_cod = u.uf_cod where uf_sigla = '" + filtro + "' order by cid_nome ";
ResultSet rs = Banco.getCon().consultar(query);
try
{
while (rs.next())
{
listmodel.addElement(rs.getString("cid_nome"));
}
rs.close();
} catch (SQLException ex)
{
System.err.println("Erro consulta :" + ex.getMessage());
}
return listmodel;
}
Estou fazendo isso para carregar o Combobox de cidades conforme o Estado
Meu problema é o seguinte quanto vou salvar essas informações da erro de Cast
Empresa e = new Empresa();
Uf estado = (Uf) this.cbbUf.getSelectedItem();
Cidade cid = (Cidade) this.cbbCidade.getSelectedItem();
e.setUf(estado);
e.setCidade(cid);
estou tomando este Exception
Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: java.lang.String cannot be cast to entidades.Uf
