Estou tentando usar o jComboBox, porem nao estou conseguindo carrega-lo usando o ArrayList!
Segue o ArrayList:
public ArrayList<Depto> listarDepto() throws SQLException {
PreparedStatement stmt = connection.prepareStatement("select ds_depto from depto");
ResultSet rs = stmt.executeQuery();
ArrayList <Depto> listaDepto = new ArrayList<Depto>();
while(rs.next()) {
Depto depto;
depto = new Depto();
//depto.setCd_depto(rs.getString("cd_depto"));
depto.setDs_depto(rs.getString("ds_depto"));
listaDepto.add(depto);
}
return listaDepto;
}
Carregando o ArrayList:
private void listaDeptoBox() throws Exception{
jComboBox1.removeAllItems();
jComboBox1.addItem("");
DeptoDAO dao = new DeptoDAO();
List<Depto> lista = dao.listarDepto();
Iterator<Depto> it = lista.iterator();
while(it.hasNext()){
Depto e = it.next();
String deptos = e.getDs_depto();
jComboBox1.addItem(deptos);
}
}
Acionando o Jcombobox:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == this.jComboBox1){
try {
listaDeptoBox();
} catch (Exception ex) {
Logger.getLogger(CadastroUsuario.class.getName()).log(Level.SEVERE, null, ex);
}
}
}