I have a table tbDocSolicitud
, which is full of information from the Sql Server database and has 18 columns.
In column number 13 Centro Costo Destino
I added a JComboBox
, called cboCentroCostoDestino
, so that it appears every time a row is added, this Combo is filled by database with a stored procedure.
Operating the program
First for each row I choose the destination cost center, I choose where the row that I select will go, once the selected rows with the respective destination cost center are already there, I press the send button.
The problem is that when the selected rows are sent it is sent with the destination cost center chosen to the last bone, the last destination cost center assigned to the last selected row is sent and the cost centers are not sent. respective for each selected row.
This is the code that I added the JComboBox
to JTable
:
TableColumn col = tbDocSolicitud.getColumnModel().getColumn(13);
col.setCellEditor(new DefaultCellEditor(cboCentroCostoDestino));
This is the code of the send button:
protected void btnEnviarActionPerformed(ActionEvent e) {
int[] selectedRow = tbDocSolicitud.getSelectedRows();
int i = tbDocSolicitud.getSelectedRow();
if(i == -1){
JOptionPane.showMessageDialog(null,"Seleccione los documentos que desea solicitar");
}
else{
for(int t : selectedRow){
String codArchivo = (String) tbDocSolicitud.getValueAt(t, 0);
String codCentroCosto = (String) tbDocSolicitud.getValueAt(t, 1);
String tipoArchivo = (String) tbDocSolicitud.getValueAt(t, 4);
String doc = (String) tbDocSolicitud.getValueAt(t, 6).toString().trim();
String nivelArchivo = (String) tbDocSolicitud.getValueAt(t, 7);
String lote = (String) tbDocSolicitud.getValueAt(t, 9);
String fila = (String) tbDocSolicitud.getValueAt(t, 10);
String usuarioSolicita = Constante.idUsuario;
Objeto object = (Objeto) cboCentroCostoDestino.getSelectedItem();
String cod_centroCostoDestino = ((Objeto)object).getCodigo();
String centroCostoOrigen = Constante.c_ccosto;
if (cod_centroCostoDestino.equals("0")){
JOptionPane.showMessageDialog(null, "Seleccione el Destino del Documento","Alerta",JOptionPane.WARNING_MESSAGE);
cboCentroCostoDestino.requestFocus();
}
else{
MovimientoArchivoDTO m = new MovimientoArchivoDTO();
m.setC_c_archivo(codArchivo);
CentroCostoDTO c = new CentroCostoDTO();
c.setC_ccosto(codCentroCosto);
m.setC_ccosto(c);
m.setC_ccosto_origen(centroCostoOrigen);
m.setC_ccosto_destino(cod_centroCostoDestino);
m.setC_tipo_doc(tipoArchivo);
m.setC_t_doc(doc);
m.setC_c_nivel_archivo(nivelArchivo);
m.setLote(lote);
m.setFila(fila);
m.setC_c_usuario_solicita(usuarioSolicita);
int estado = x.RegistrarSolicitud_SA(m);
if (estado == 1){
documentos = doc.split(" ");
for (String value : documentos) {
notificacion = notificacion + "-" + value + " " + "para" + " " + cod_centroCostoDestino + " \n";
}
}
else{
mensaje("Error en enviar");
}
}
}
JOptionPane.showMessageDialog(null, new Object[] {notificacion}, "Solicitud Enviada", 1, null);
ListarDocumentoSA(estado_flg);
}
}
Everything is sent well but as I say the problem is that for each row is not sent with its respective destination cost center that I choose, it is sent with the last destination cost center that I assign to the last row.
In this image I am selecting and assigning your document destination (destination cost center that comes to be a JComboBox
) to each row you want to send on request:
Code with which full of information at JComboBox
cboCentroCostoDestino
:
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();
}
}
}