I have implemented jtable with checkbox. The information comes from a database. When I aplly the rendering I get an error. I don't know understand the cause of the error.
public void obtenerUltimoRegistro(){ Object []Registros= new Object[18];
String Sql= "SELECT * FROM factura INNER JOIN cliente ON factura.idCliente = cliente.idCliente INNER JOIN detalle_factura ON factura.idFactura = detalle_factura.idFactura INNER JOIN producto ON detalle_factura.idProducto=Producto.idProducto WHERE factura.idFactura=(SELECT MAX(idFactura) FROM factura)";
try {
Statement sql = cn.createStatement();
ResultSet rs = sql.executeQuery(Sql);
int fila=-1;
while(rs.next())
{
fila++;
Registros[0]=rs.getString("idFactura");
Registros[1]=rs.getString("idCliente");
Registros[2]=rs.getString("fecha");
Registros[3]=rs.getString("subtotal");
Registros[4]=rs.getString("descuento");
Registros[5]=rs.getString("iva");
Registros[6]=rs.getString("total");
Registros[7]=rs.getString("ci_ruc");
Registros[8]=rs.getString("nombres");
Registros[9]=rs.getString("apellidos");
Registros[10]=rs.getString("direccion");
Registros[11]=rs.getString("telefono");
Registros[12]=rs.getString("idProducto");
Registros[13]=rs.getString("cantidad");
Registros[14]=rs.getString(18); //xq hay dos nombres total, se hace referencia al numero de columna
Registros[15]=rs.getString("descripcion");
Registros[16]=rs.getString("pvp");
Registros[17]=rs.getBoolean(26);
//Convertir formato de fecha
java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String)Registros[2]);
lblNumFactura.setText((String)Registros[0]);
txtCodigoCliente.setText((String)Registros[7]);
jTextArea1.setText(Registros[8]+"\n"+Registros[9]+"\n"+Registros[10]+"\n"+Registros[11]);
jDateChooser1.setDate(date);
lblSubtotal.setText((String)Registros[3]);
lblDescuento.setText((String)Registros[4]);
lblIva.setText((String)Registros[5]);
lblTotal.setText((String)Registros[6]);
//Para cargar el detalle de la factura
modelo.setValueAt(Registros[12], fila, 0);
modelo.setValueAt(Registros[15], fila, 1);
modelo.setValueAt(Registros[13], fila, 2);
modelo.setValueAt(Registros[16], fila, 3);
modelo.setValueAt(Registros[14], fila, 4);
modelo.setValueAt(Registros[17], fila, 5);
}
//Para agregar checkbox a la columna iva
jTable.getColumnModel().getColumn(5).setCellRenderer(new CellRenderer());
} catch (SQLException ex) {
Logger.getLogger(Frm_Factura.class.getName()).log(Level.SEVERE, null, ex);
}
catch(Exception e){
}
}
The code of CellRenderer is:
public class CellRenderer extends DefaultTableCellRenderer {
private JCheckBox check = new JCheckBox();
/** Constructor de clase*/
public CellRenderer(){}
@Override
public Component getTableCellRendererComponent ( JTable table, Object value, boolean selected, boolean focused, int row, int column )
{
//color de celda seleccionada
this.setBackground( (selected)? new Color( 50, 153 , 254):new Color(255,255,255) );
//
Boolean bol = Boolean.valueOf(String.valueOf(value));
if( value == null )
{
return null;
}
else // es un boolean
{
check = new JCheckBox();
check.setHorizontalAlignment( JLabel.CENTER );
check.setBackground( (selected)? new Color( 50, 153 , 254):new Color(255,255,255) );
check.setSelected( bol ); //valor de celda
return check;
}
}
Thanks for your help
Aucun commentaire:
Enregistrer un commentaire