Tenho minha combobox UF E minha combobox Municipio
queria que ao clicar na combobox UF listar na combobox Municipio apenas os municipios inseridos pela sigla UF
Caso alguem possa me ajudar agradeco abraços.
public BairroEscolherUfAction(JComboBox<Object> ufJComboBox,
JComboBox<Object> municipioJComboBox, CadastroBairroView view,
BairroEscolherMunicipioAction view3) {
super();
this.ufJComboBox = ufJComboBox;
this.municipioJComboBox = municipioJComboBox;
this.municipioService = new DefaultMunicipioService();
this.view = view;
this.view3 = view3;
}
// Evento que da ação a combobox UF
@Override
public void actionPerformed(ActionEvent euf) {
UFVO ufvo = (UFVO) view.getUfJComboBox().getSelectedItem();
// METODO TRY CATCH EM CASO DE EXECOES JUNTO COM A CONDIÇÃO if e else
// sendo assim se UF não for selecionado return mensagem
try {
view.getMunicipioJComboBox().removeAllItems();
if (view.getUfJComboBox().getSelectedItem() != null) {
CarregaObjetosnacombo(nome, ufvo);
} else {
JOptionPane.showMessageDialog(view, "UF não selecionada");
}
} catch (BairroException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(view, "erro1");
} catch (DataBaseException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(view, "erro2");
}
}
// metodo responsavel por carregar operacoes no evento Action performed da
// combobox
public void CarregaObjetosnacombo(CharSequence nome, UFVO ufvo)
throws BairroException, DataBaseException {
ResultSet rs = null;
PreparedStatement query = null;
Connection connection = null;
String sql = null;
try {
connection = DataSource.getConnection();
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/cliente-db", "impacta",
"impacta");
sql = "SELECT nm_municipio,id_municipio ,id_uf FROM municipio";
try {
query = connection.prepareStatement(sql);
rs = query.executeQuery();
query.setString(1, String.valueOf(rs.getString("id_uf")));
} catch (Exception e) {
}
List<Municipio> listMunicipios = new ArrayList<Municipio>();
List<UFVO> listUfvos = new ArrayList<UFVO>();
while (rs.next()) {
Municipio municipio = new Municipio();
//query.setString(1, String.valueOf(rs.getString("id_uf")));
municipio.setId(rs.getInt("id_municipio"));
municipio.setNome(rs.getString("nm_municipio"));
municipio.setUF(ufvo);
view.getUfJComboBox().addItem(ufvo.isSelecionado());
view.getMunicipioJComboBox().addItem(
rs.getString("nm_municipio"));
listMunicipios.add(municipio);
listUfvos.add(ufvo);
}
for (Municipio municipio : listMunicipios) {
System.out.println(municipio.getId());
System.out.println(view.getUfJComboBox());
if (ufvo.isSelecionado()) {
view.getMunicipioJComboBox().setModel(new DefaultComboBoxModel<>());
view.getMunicipioJComboBox().setSelectedItem(municipio.getNome());
view.getMunicipioJComboBox().addItem(municipio.compareTo(municipio));
}
if (view.getUfJComboBox().getSelectedItem() == null) {
view.getMunicipioJComboBox().removeAllItems();
}
/**if (view.getUfJComboBox().getSelectedItem().equals(municipio.getNome())) {
view.getMunicipioJComboBox().setSelectedItem(rs.getString("nm_municipio"));
}
if (ufvo.equals(municipio.getNome())) {
view.getMunicipioJComboBox().getSelectedIndex();
view.getMunicipioJComboBox().removeAllItems();
}
if (view.getMunicipioJComboBox().getSelectedItem().equals(ufvo)) {
view.getMunicipioJComboBox().removeAllItems();
}
if (view.getUfJComboBox().getSelectedIndex() > 0) {
}else if (view.getMunicipioJComboBox().getSelectedItem() == municipio.getNome()) {
view.getMunicipioJComboBox().removeAllItems();
}*/
}
} catch (SQLException cause) {
cause.printStackTrace();
JOptionPane.showMessageDialog(null, "Não foi possivel Carregar");
}
}