CheckBox in a JTable - Java [duplicated]

0

How to do so that when I select the checkbox of one row and select another checkbox from another row it is stored in a variable and when I click the send button it is sent to another table to be validated.

This is the creation code of the table:

tbDocTransferir.setModel(new DefaultTableModel(
            new Object[][] {
            },
            new String[] {
                "C. Costo", "Cod. SD", "Tipo de Archivo", "N\u00B0 Doc.", "Permanencia", "Tipo de Nivel de Archivo", "Per. Ret.", "Solicitar"
            }
        ) {
            @SuppressWarnings("rawtypes")
            Class[] columnTypes = new Class[] {
                Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Object.class, Boolean.class
            };
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
            boolean[] columnEditables = new boolean[] {
                false, false, false, false, false, false, false, true
            };
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        tbDocTransferir.getColumnModel().getColumn(0).setPreferredWidth(93);
        tbDocTransferir.getColumnModel().getColumn(1).setPreferredWidth(87);
        tbDocTransferir.getColumnModel().getColumn(2).setPreferredWidth(125);
        tbDocTransferir.getColumnModel().getColumn(4).setPreferredWidth(95);
        tbDocTransferir.getColumnModel().getColumn(5).setPreferredWidth(150);
        tbDocTransferir.getColumnModel().getColumn(6).setPreferredWidth(92);
        tbDocTransferir.getColumnModel().getColumn(7).setPreferredWidth(61);
        tbDocTransferir.setDefaultRenderer(Object.class, new Mirender());
        scrollPane.setViewportView(tbDocTransferir);
        tbDocTransferir.setRowSelectionAllowed(true);
        tbDocTransferir.setColumnSelectionAllowed(false);

This code is so that the information that is in the database is listed in the table:

void ListarDocumento(String centroCosto){
            ArrayList<ServicioArchivisticoDTO> doc = x.ListarDocumento(centroCosto);
            DefaultTableModel model = (DefaultTableModel) tbDocTransferir.getModel();
            model.setRowCount(0);
            for (ServicioArchivisticoDTO s : doc){
                Object fila[] = {s.getCentroCosto().getC_t_costo(), s.getSerieDocumental().getC_c_sd(), s.getTipoArchivo().getC_t_tipo_archivo(),
                                    s.getArchivo().getC_t_doc(), s.getSerieDocumental().getN_i_tiempo_retencion()+" años", s.getNivelArchivo().getC_t_nivel_archivo(),
                                    s.getPeriodo_retencion()};
                model.addRow(fila);
            }
        }
    
asked by Bruno 12.01.2017 в 15:42
source

2 answers

0

I have not used Swing in Java for a long time, but if I remember correctly, you have to implement a listener in the checkbox in order to retrieve the object you are labeling. From there you could add it to a list and fill the other table. Greetings!

    
answered by 12.01.2017 в 21:20
0

Using the TableModelListener function, you could do the following, using getRowCount() to get the total number of rows in your table and getValueAt() which will get the value in a given position.

//obtenemos el modelos de tu tabla y le agregamos el listener
tbDocTransferir.getModel().addTableModelListener(new checkBoxListener());

public class checkBoxListener implements TableModelListener {

   public void tableChanged(TableModelEvent e) {
    int fila = e.getFirstRow();
    int columna = e.getColumn();
    //verifica si la columna es de tipo boolean
    if (columna == BOOLEAN_COLUMN) {
        /*
            obtenemos el modelo de la tabla
            este te servira al igual para obtener los valores de tu tabla,
            dependiendo de la fila y columna que desees tomar.
        */
        TableModel model = (TableModel) e.getSource();
        Boolean checked = (Boolean) model.getValueAt(fila, columna);
        //verifica si esta seleccionada
        if (checked) {
            //aqui haces la insercion del registro a una nueva tabla
        }
   }
}
    
answered by 12.01.2017 в 21:55