I'm trying to add an object in jCombo box, but, it's saying by showing the message that Object can not be converted to String, I'm using public String toString()
Follow the model's code:
public class Model(){
private int codigo;
private String nome;
public int getCodigo(){ return this.codigo; }
public void setCodigo(int codigo){ this.codigo = codigo; }
public String getNome(){ return this.nome; }
public void setNome(String nome){ this.nome = nome; }
@Override
public String toString(){ return nome; }
}
From DAO:
public class Model_DAO {
public List<Model> getListModel(){
List lista = new ArrayList();
connection = ConnectionFactory.getConnection();
sql = "SELECT id, nome FROM model ";
try {
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
while( rs.next() ){
Model model = new Model();
model.setId( rs.getInt( "id" ) );
model.setNome( rs.getString("nome") );
lista.add( model );
}
connection.close();
} catch (SQLException ex) {
Logger.getLogger(EnergiaDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return lista;
}
}
Combobox Filling
public class TelaClasse {
public TelaClasse(){
popularComboMedidor();
JFrame tela = new JFrame();
tela.add( jc_model );
}
JCombobox jC_model = new JComboBox();
private void popularComboMedidor(){
Model_DAO md = new Model_DAO();
List lista = md.getListModel();
Iterator it = lista.iterator();
jC_model.removeAllItems(); //JC jCombobox
while( it.hasNext() ){
Model model = (Model) it.next();
// System.out.println(model);
jC_model.addItem( model );
}
}
}
Remembering that I always did this, this other computer I'm using is not working. The toString()
method is only working with System.out.println()