Add Object to Jcombobox

Asked

Viewed 76 times

0

I’m trying to add an object in the jCombo box, but, it’s saying showing the message that the Object cannot be converted to String, being that I’m using the public String toString()

Follow the Model 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; }
}

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;
        }
}

Completion of the Combobox

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 have always done so, in this other computer that I am using is not working. The method toString() is only working with System.out.println()

  • This has already been answered here, from a look at the two answers of the marked question, one of the two will be the solution, for sure.

  • Yeah, I added the method toString() and it hasn’t worked yet

  • Have two answers there, see both. If none serve, Oce need to provide [mcve] because this code is neither executable, therefore, can not know the origin of the error.

  • Exactly, it shows no error. The main methods I put. Can you suggest me what else is missing? Because if it were on the web I would use that stackoverflow tool itself to show

  • Access the link I sent, it explains well how to elaborate a mcve.

  • Your Model is not using toString and yes getString. This is not the correct method to overwrite.

  • True, I typed wrong, but I’ve corrected

  • Look, from what I’m seeing, your question remains duplicate, the solution of diego Ugusto solves your problem. jcombobox cannot receive Model because he does not know this class. You will have to make it known to him, and this is only possible by creating an Abstractlistmodel.

  • As I told you, I have used this way countless times, but just today it didn’t work. Your method is much simpler

  • But it will not do the way you are doing, if you do not want to do it in the simple way, you can not pass Model pro combobox, for reasons already explained.

  • Actually I prefer to do the way you did answered there, which is the pattern I’ve always used, but .... I don’t know what happened there

  • 1

    I have explained what happened 2 times, please review my previous comments.

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.