Unwanted value when printing the value of a JComboBox in a JTable - Java

0

I have a Jtable ( tbDocSolicitud ) that is filled with information from the database Sql Server 2008 R2 . It has 10 columns and in the first column there is a CheckBox and in the second column there is a ComboBox that is filled from the database, the problem is that when I check the rows of the Jtable and at the same time I select the combobox and when I click on the send button, I do not print the value of the combobox, I mean its code if I do not get its name from the combobox.

This is the code where I filled the JComboBox:

void CargarCentroCostoDestino() throws Exception{
        Connection cn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            combo = new DefaultComboBoxModel<Objeto>();
            cn = new SqlConexion().getConectar();
            String sql = "Select c_ccosto, x_ccosto from fccosto";
            pstm = cn.prepareStatement(sql);
            rs = pstm.executeQuery();
            combo.addElement(new Objeto("0", "Seleccione"));
            while(rs.next()){
                combo.addElement(new Objeto(rs.getString(1), rs.getString(2)));
            }
            cboCentroCostoDestino.removeAllItems();
            cboCentroCostoDestino.setModel(combo);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try {
                if (rs != null)
                    rs.close();
                if (pstm != null)
                    pstm.close();
                if (cn != null)
                    cn.close();
            } 
            catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

This is the Object class:

public class Objeto {
    private String codigo;
    private String nombre;

    public Objeto(String codigo, String nombre){
        this.codigo = codigo;
        this.nombre = nombre;
    }

    public Objeto() {}

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    @Override
    public String toString(){
        return nombre;
    }
} 

This is the code of the send button:

protected void btnEnviarActionPerformed(ActionEvent e) {
        int cont = 0;
        Boolean checked = false;
        Objeto object = new Objeto();
        for(int i = 0; i < tbDocSolicitud.getRowCount(); i++){
            checked = Boolean.valueOf(tbDocSolicitud.getValueAt(i, 0).toString());
            object.setCodigo(tbDocSolicitud.getValueAt(i, 1).toString());
            String cod_centroCostoDestino = object.getCodigo();
            if(checked == true){
                cont++;
                System.out.println(cod_centroCostoDestino);
            }
        }
        if(cont == 0){
            JOptionPane.showMessageDialog(null, "Marque los documentos que desea solicitar");
            return;
        }
    }
    
asked by Angelica 19.05.2017 в 17:19
source

1 answer

0

Replace the line - > combo = new DefaultComboBoxModel (); by:

     combo = new DefaultComboBoxModel<Objeto>(){
        public String toString() {
            return this.getSelectedItem() != null ? this.getSelectedItem().toString() : null;   
        }
    };
    
answered by 19.05.2017 в 22:41