Problem when passing information from a JTable to a JComboBox

0

I have a JTable full of information from the database sqlServer , when I select a row I want the data from one column to be passed to JComboBox cboCentroCosto , but I get the following error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to Adicional.Objeto
    at Vista.frmControlDocumentos.tbSerieDocumentalMouseClicked(frmControlDocumentos.java:754)
    at Vista.frmControlDocumentos$6.mouseClicked(frmControlDocumentos.java:329)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)

This is the code that filled the cboCentroCosto:

void CargarCentroCosto() 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)));
            }
            cboCentroCosto.removeAllItems();
            cboCentroCosto.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 event when you click on a row:

public void tbSerieDocumentalMouseClicked(MouseEvent e) {
        int fila = tbSerieDocumental.getSelectedRow();

        Objeto object = (Objeto) tbSerieDocumental.getValueAt(fila, 1);
        cboCentroCosto.setSelectedItem(object.getNombre());
    }

This is my Object class:

package Adicional;

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

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

    public String getCodigo(){
        return codigo;
    }

    public String getNombre(){
        return nombre;
    }

    @Override
    public String toString(){
        return nombre;
    }
}
    
asked by Angelica 28.04.2017 в 00:37
source

1 answer

0

The method getValueAt returns a Objeto of type String in this case , but when cast (Objeto) shows that error, to solve it could create an object and assign it the value returned by getValueAt to an attribute of the class. Which incidentally needs its Setters to the Class Objeto , to later access that attribute when assigning a setSelectedItem of the combo.

public void tbSerieDocumentalMouseClicked(MouseEvent e) {
    int fila = tbSerieDocumental.getSelectedRow();
    Objeto object = new Objeto();/* Crea el Objeto*/
    object.setNombre(tbSerieDocumental.getValueAt(fila, 1).toString());
    cboCentroCosto.setSelectedItem(object.getNombre());
}

Do not forget that you must create the constructor without parameters for your class. And its setters

      package Adicional;

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

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

        public Objeto() {
          } 

        public void setNombre(String name) {
              this.nombre =name;
          } 
          public void setCodigo(String code) {
               this.codigo = code;
          } 
    
answered by 28.04.2017 в 00:46