Unselect row by deselecting the checkbox in a jtable

2

I have 2 classes that convert a column into a checkbox.

The problem is that when removing the selected one from a checkbox, the row is still painted, and that affects me in some methods. I need that when removing the selected one to the row the painted one is removed

these are the classes I use:

package Presentacion;

import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class Clase_CellEditor extends DefaultCellEditor implements TableCellRenderer{
    public final JComponent component = new JCheckBox();    
    public boolean value = false; // valor de la celda

    /** Constructor de clase */
    public Clase_CellEditor() {
        super( new JCheckBox() );
    }

    /** retorna valor de celda
     * @return  */
    @Override
    public Object getCellEditorValue() {
        return ((JCheckBox)component).isSelected();        
    }

    /** Segun el valor de la celda selecciona/deseleciona el JCheckBox
     * @param table
     * @param value
     * @param isSelected
     * @param row
     * @param column
     * @return  */
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        //Color de fondo en modo edicion
        ( (JCheckBox) component).setBackground( new Color(200,200,0) );
        //obtiene valor de celda y coloca en el JCheckBox
        boolean b = ((Boolean) value).booleanValue();
        ( (JCheckBox) component).setSelected( b );
        return ( (JCheckBox) component);     
    }

    /** cuando termina la manipulacion de la celda
     * @return  */
    @Override
    public boolean stopCellEditing() {        
        value = ((Boolean)getCellEditorValue()).booleanValue() ;
        ((JCheckBox)component).setSelected( value );
        return super.stopCellEditing();
    }

    /** retorna componente
     * @return  */
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
         if (value == null)
            return null;         
         return ( (JCheckBox) component );
    }
}

and this is the other one

package Presentacion;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Efrainrodc
 */
public class Clase_CellRender extends JCheckBox implements TableCellRenderer{
    private final JComponent component = new JCheckBox();

    /** Constructor de clase */
    public Clase_CellRender() {
        setOpaque(true);
    }

    @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      //Color de fondo de la celda
      ( (JCheckBox) component).setBackground( new Color(167,218,251) );//[167,218,251]

      //obtiene valor boolean y coloca valor en el JCheckBox
      boolean b = ((Boolean) value).booleanValue();
      ( (JCheckBox) component).setSelected( b );

      return ( (JCheckBox) component);
  }
}
    
asked by Efrainrodc 29.12.2016 в 21:22
source

1 answer

1

In theory just putting a if in the method where the event occurs select / deselect check box should work. I do not see the method that manages the JCheckbox events.

I give you a web where you will learn how to add the methods that listen to events with a JCheckbox .

link

Add this code in the listener:

public void actionPerformed(ActionEvent e) {
       JCheckBox cb = (JCheckBox) e.getSource();
       if(cb.isSelected()){
            ( (JCheckBox) component).setBackground( new Color(167,218,251) ); 
       }else{
            ( (JCheckBox) component).setBackground( new Color(0,0,0) );
       }
}

answered by 30.12.2016 в 17:19