Problem of a JComboBox in a JTable

1

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();
            }
        }
    }
    
asked by Angelica 18.04.2017 в 22:27
source

2 answers

1
  

UPDATE 1:

Well, I thought that the combo only worked as a function to fill in the rows and it would work for you, try the following:

Objeto object = (Objeto) tbDocSolicitud.getValueAt(t, 13);
String cod_centroCostoDestino = object.toString(); 

or

Objeto object = (Objeto) tbDocSolicitud.getValueAt(t, 13); y 
String cod_centroCostoDestino = object.getCodigo();

and comment. I regret that it did not work for him, he was almost convinced that it would work for him.

  

UPDATE:

I have not worked with this type of applications for a long time, but try this change:

    //String cod_centroCostoDestino = ((Objeto)object).getCodigo(); <-- pero no se que recupera getCodigo(); aun asi intente el cambio 
    //Objeto object = (Objeto) cboCentroCostoDestino.getSelectedItem();
    String cod_centroCostoDestino = tbDocSolicitud.getValueAt(t, 13); 

You can also look at this:

    String cod_centroCostoDestino = tbDocSolicitud.getModel().getValueAt(t, 13);

Remembering a bit to fix me better I think that the JComboBox is used to edit the cell and it does not contain the data for the cell, in question, so that, when obtaining the value, obtain the last one that was selected in the combo_box, I still have the doubt that returns in one of its lines .getCodigo();

    
answered by 19.04.2017 / 00:11
source
0

Try taking the value of the combo in the selected row. such that:

Objeto object = (Objeto) cboCentroCostoDestino.getItemAt(t);
String cod_centroCostoDestino = ((Objeto)object).getCodigo();
    
answered by 19.04.2017 в 18:15