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