Deactivate Button in Jtable

0

I took some time doing a table in Java. The issue is that I need help, since I need to "disable" a button (which is integrated in the table) if in the comment COLUMN corresponds to "Rejection".

I hope you can help me. I break the code.

public void llamarMuestra() throws SQLException {
 //   tblDatos.setDefaultRenderer (Object.class, new MiRender()); 
    tblDatos.setDefaultRenderer(Object.class, new Render());   //LLAMA CLASE RENDER QUE GENERA LOS BOTONES
    tblDatos.setAutoResizeMode(JTable.WIDTH);

    setVisible(true);
    JButton btn1 = new JButton("Modificar");
    btn1.setName("modificar");
    JButton btn2 = new JButton("Eliminar");
    btn2.setName("eliminar");
    DefaultTableModel modelo = new DefaultTableModel();
    JTable tabla = new JTable(modelo);

    trsfiltro = new TableRowSorter(modelo);
    tblDatos.setRowSorter(trsfiltro);
     String query = ("SELECT m.idMuestras, m.nregistro, c.nombrecliente, m.matriz, m.fechaingresolaboratorio, m.laboratorioejecutante, m.estado, m.observaciones,m.comentario "
            + "FROM muestras m "
            + "INNER JOIN clientes c ON m.fk_idcliente = c.idClientes WHERE m.laboratorioejecutante LIKE 'LADIOX' ");
    PreparedStatement pst = cn.prepareStatement(query);
    ResultSet ResultSet = pst.executeQuery();
    modelo.setColumnIdentifiers(new Object[]{"ID Muestras", "Numero Registro", "Cliente", "Matriz", "Analisis", "Ingreso Laboratorio", "Laboratorio Ejecutante", "Estado", "Comentario Rechazo", "Modificar", "Eliminar"});  //creo el nombre de la columna
        try {
            while (ResultSet.next()) {
                int a = ResultSet.getInt("m.idMuestras");
                modelo.addRow(new Object[]{ResultSet.getInt("m.idMuestras"), ResultSet.getInt("m.nregistro"), ResultSet.getString("c.nombrecliente"),
                    ResultSet.getString("m.matriz"), Listaanalisis(a), ResultSet.getDate("m.fechaingresolaboratorio"),
                    ResultSet.getString("m.laboratorioejecutante"), ResultSet.getString("m.estado"), ResultSet.getString("m.comentario"), btn1, btn2});
                btn2.repaint();
            }
            tblDatos.setModel(modelo);
            tblDatos.getColumnModel().getColumn(0).setMaxWidth(0);
            tblDatos.getColumnModel().getColumn(0).setMinWidth(0);
            tblDatos.getColumnModel().getColumn(0).setPreferredWidth(0);
            modelo.fireTableDataChanged();
        } catch (SQLException e) {
            Logger.getLogger(TableroMuestraLADIOX.class.getName()).log(Level.SEVERE, null, e);
            System.out.println("error con tabla");
        }
    }

WITH THE CODE GET UP THE TABLE AND CHARGE THE DATA AND NOW I SHARE THE RENDER FOR THE CREATION OF THE BUTTON WITHIN THE TABLE

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;

public class Render extends DefaultTableCellRenderer {

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {


        // CREA BOTON DENTRO DE TABLA
        if (value instanceof JButton) {
            JButton btn = (JButton) value;
            if (isSelected) {
                btn.setForeground(table.getSelectionForeground());
                btn.setBackground(table.getSelectionBackground());
            } else {
                btn.setForeground(table.getForeground());
                btn.setBackground(UIManager.getColor("Button.background"));
            }
            return btn;
        }
        if (value instanceof JCheckBox) {
            JCheckBox ch = (JCheckBox) value;
            return ch;
        }


        return super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column); //To change body of generated methods, choose Tools | Templates.
    }
//        return null;


    }
    
asked by Juan Pablo 28.11.2018 в 15:57
source

1 answer

0

Only use the setEnable method before adding these buttons to the model of the table, being as follows:

btn1.setEnabled(false);
btn2.setEnabled(false);

The complete code would be as follows:

public void llamarMuestra() throws SQLException {
 //   tblDatos.setDefaultRenderer (Object.class, new MiRender()); 
    tblDatos.setDefaultRenderer(Object.class, new Render());   //LLAMA CLASE RENDER QUE GENERA LOS BOTONES
    tblDatos.setAutoResizeMode(JTable.WIDTH);

    setVisible(true);
    JButton btn1 = new JButton("Modificar");
    btn1.setName("modificar");
    JButton btn2 = new JButton("Eliminar");
    btn2.setName("eliminar");
    DefaultTableModel modelo = new DefaultTableModel();
    JTable tabla = new JTable(modelo);
    btn1.setEnabled(false);
    btn2.setEnabled(false);
    trsfiltro = new TableRowSorter(modelo);
    tblDatos.setRowSorter(trsfiltro);
     String query = ("SELECT m.idMuestras, m.nregistro, c.nombrecliente, m.matriz, m.fechaingresolaboratorio, m.laboratorioejecutante, m.estado, m.observaciones,m.comentario "
            + "FROM muestras m "
            + "INNER JOIN clientes c ON m.fk_idcliente = c.idClientes WHERE m.laboratorioejecutante LIKE 'LADIOX' ");
    PreparedStatement pst = cn.prepareStatement(query);
    ResultSet ResultSet = pst.executeQuery();
    modelo.setColumnIdentifiers(new Object[]{"ID Muestras", "Numero Registro", "Cliente", "Matriz", "Analisis", "Ingreso Laboratorio", "Laboratorio Ejecutante", "Estado", "Comentario Rechazo", "Modificar", "Eliminar"});  //creo el nombre de la columna
        try {
            while (ResultSet.next()) {
                int a = ResultSet.getInt("m.idMuestras");
                modelo.addRow(new Object[]{ResultSet.getInt("m.idMuestras"), ResultSet.getInt("m.nregistro"), ResultSet.getString("c.nombrecliente"),
                    ResultSet.getString("m.matriz"), Listaanalisis(a), ResultSet.getDate("m.fechaingresolaboratorio"),
                    ResultSet.getString("m.laboratorioejecutante"), ResultSet.getString("m.estado"), ResultSet.getString("m.comentario"), btn1, btn2});
                btn2.repaint();
            }
            tblDatos.setModel(modelo);
            tblDatos.getColumnModel().getColumn(0).setMaxWidth(0);
            tblDatos.getColumnModel().getColumn(0).setMinWidth(0);
            tblDatos.getColumnModel().getColumn(0).setPreferredWidth(0);
            modelo.fireTableDataChanged();
        } catch (SQLException e) {
            Logger.getLogger(TableroMuestraLADIOX.class.getName()).log(Level.SEVERE, null, e);
            System.out.println("error con tabla");
        }
    }

I recommend that in each row you create several instances of buttons, not only btn1 or btn2, because if you want to add an event that is different for each of these, you will have problems, since they will be practically objects under the same instance, but that already depends on you and what you want to do.

    
answered by 07.12.2018 в 02:45