Ao selecionar minha UF view.getUfcombobox ele diretamente fazer um select na outra combobox view.getMunicipio.combobox
sendo assim… o unico problema é que esta me retornando um valor NULL,NULL.
não entendi o por que.
Preciso de ajuda. pois só falta isso para terminar esse projeto.
Segue classe Escolher bairroescolherUF
public class BairroEscolherUfAction implements ActionListener,
Comparator<Bairro> {
private static final UFVO uf = null;
private JComboBox<Object> ufJComboBox;
private JComboBox<Object> municipioJComboBox;
private MunicipioService municipioService;
private CadastroBairroView view;
private BairroEscolherMunicipioAction view3;
private CharSequence nome;
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;
}
ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent itemEvent) {
int Status = itemEvent.getStateChange();
System.out.println((Status == ItemEvent.SELECTED) ? "SELECECIONADO"
: "DESELECIONADO");
System.out.println("item UF: " + itemEvent.getItem());
ItemSelectable is = itemEvent.getItemSelectable();
System.out.println("selecionado" + toString());
view.getUfJComboBox().addItem(itemEvent);
view.getMunicipioJComboBox().addItem(itemEvent);
}
};
@Override
public void actionPerformed(ActionEvent euf) {
try {
view.getMunicipioJComboBox().removeAllItems();
if (view.getUfJComboBox().getSelectedItem() != null) {
Bairro domain;
domain = new Bairro();
CarregaObjetosnacombo(domain);
}
} catch (BairroException e) {
e.printStackTrace();
}
}
public void CarregaObjetosnacombo(Bairro bairro) throws BairroException {
Collection<Bairro> municipios = new TreeSet<>();
List<Municipio> lisMunicipios = new ArrayList<Municipio>();
Set<Object> nomes;
nomes = new HashSet<>();
ResultSet rs = null;
Connection connection = null;
String sql = null;
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/cliente-db", "impacta",
"impacta");
Statement query = (Statement) connection.createStatement();
sql = "select * from municipio order by nm_municipio";
/**
* SELECT bairro.id_bairro,bairro.nm_bairro,municipio.id_municipio,
* municipio.nm_municipio FROM bairro" + "INNER JOIN municipio " +
* "ON municipio.id_municipio = bairro.municipio_id" +
* "WHERE municipio.id_uf=? AND bairro.nm_bairro =?"
*/
// SELECT * FROM municipio ORDER BY id_uf
rs = query.executeQuery(sql);
while (rs.next()) {
Municipio municipio = new Municipio();
// view.getMunicipioJComboBox().addItem(
// rs.getString("nm_municipio"));
view.getService().listar(municipio);
view.getService().listar(nome, uf);
view.getMunicipioJComboBox().addItem(municipio);
lisMunicipios.add(municipio);
}
for (int i = 0; i < lisMunicipios.size(); i++) {
if (view.getUfJComboBox().getSelectedItem() != null) {
view.getService().listar(nome, uf);
} else {
JOptionPane.showMessageDialog(view, "UF não selecionada");
}
}
/** Iterator<?> i = lisMunicipios.iterator(); */
/**
* while (i.hasNext()) { i.next();
*
*
* }
*/
} catch (SQLException cause) {
cause.printStackTrace();
JOptionPane.showMessageDialog(null, "Não foi possivel Carregar");
}
}
public JComboBox<Object> getUfJComboBox() {
return ufJComboBox;
}
public void setUfJComboBox(JComboBox<Object> ufJComboBox) {
this.ufJComboBox = ufJComboBox;
}
public JComboBox<Object> getMunicipioJComboBox() {
return municipioJComboBox;
}
public void setMunicipioJComboBox(JComboBox<Object> municipioJComboBox) {
this.municipioJComboBox = municipioJComboBox;
}
public MunicipioService getMunicipioService() {
return municipioService;
}
public void setMunicipioService(MunicipioService municipioService) {
this.municipioService = municipioService;
}
public CadastroBairroView getView() {
return view;
}
public void setView(CadastroBairroView view) {
this.view = view;
}
public BairroEscolherMunicipioAction getView3() {
return view3;
}
public void setView3(BairroEscolherMunicipioAction view3) {
this.view3 = view3;
}
public CharSequence getNome() {
return nome;
}
public void setNome(CharSequence nome) {
this.nome = nome;
}
public ItemListener getItemListener() {
return itemListener;
}
public void setItemListener(ItemListener itemListener) {
this.itemListener = itemListener;
}
@Override
public int compare(Bairro obj1, Bairro obj2) {
int comp = obj1.getUf().compareTo(uf);
if (comp == 0) {
comp = obj1.getMunicipio().compareTo(obj2.getMunicipio());
}
return comp;
}
}