This discussion is archived
4 Replies Latest reply: Apr 18, 2013 12:58 AM by PhHein RSS

Checkbox selected : row not rendering

1003640 Newbie
Currently Being Moderated
I am not able to set colors for the row that has checked checkboxes. I want to set red color for the row having
checked checkboxes and green for unchecked.
table.getColumnModel().getColumn(3).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
 table.getColumnModel().getColumn(3).setCellRenderer(new CustomTableCellRenderer3());

''''''''''''''''''''''''''''''''''''''''''''''''''''''

setcellEditor:
package moxaclient;
 
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
 
public class CustomTableCellRenderer extends DefaultCellEditor implements ItemListener{
 
private static final long serialVersionUID = 1L;
private JCheckBox checkBox;
private JTable table1;
private int row;
private int column;
Object abc="null";
private String Name;
Component comm=null;
 
public CustomTableCellRenderer(JCheckBox checkBox,String name2) {
    super(checkBox);
    this.checkBox = checkBox;
    this.checkBox.addItemListener(this);
    this.Name=name2;   
}
 
 
public Component getTableCellEditorComponent(JTable table, Object value,
        boolean isSelected,int row, int column) 
{
    Component comp = super.getTableCellEditorComponent(table, value, isSelected, row, column);
    this.row = row;
    this.table1=table;
    this.column = column;
    checkBox.setSelected((Boolean)value);
    return super.getTableCellEditorComponent(table, value, isSelected,row, column);
}
 
public void itemStateChanged(ItemEvent e) 
   {
   this.fireEditingStopped(); 
    //System.out.println("Item Changed " + row + " value is: " + checkBox.isSelected());
     //System.out.println("Item Changed " + column + " value is: " + checkBox.isSelected());
      String Sensor =(String) table1.getValueAt(row, 0);
      Double Value =(Double) table1.getValueAt(row, 1);
      String Date =(String) table1.getValueAt(row, 2);
      Boolean select=(Boolean) table1.getValueAt(row,3);
       if (Boolean.TRUE.equals(select)) 
       {
        abc += Sensor+"\t"+Value+"\t"+Name+"\t"+Date+"\t";
        // table1.set Background(Color.black);
        //  checkBox.setBackground(Color.red);
          // comm.setBackground(Color.red);
    Clientthread ct=new Clientthread(abc);
    }
    }
    }
 
   }


'''''''''''''''''''''''''''''''''''''''''''

package moxaclient;
 
import java.awt.Component;
import java.util.EventObject;
 
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
 
public class CustomTableCellRenderer3 extends DefaultTableCellRenderer {
JTable table1=null;
DefaultTableModel model;
    public CustomTableCellRenderer3() {
        // TODO Auto-generated constructor stub
    }
 
    public CustomTableCellRenderer3(JTable table,DefaultTableModel model1) {
        this.table1=table;
        this.model=model1;
    }
 
 
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) 
    {
         Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
         Boolean last=(Boolean)model.getValueAt(row,3);
                   if(last)
                    {
                         //setcolor dosent work here
                    }
                      
                return null;
                 }
     
}
Edited by: 1000637 on Apr 17, 2013 5:29 AM
  • 1. Re: Checkbox selected : row not rendering
    thomas.behr Newbie
    Currently Being Moderated
    I suggest first trying to get cell renderers to work as expected before going onto cell editors. See Using Custom Renderers in the Swing tutorial for basic information on how to use cell renderers. That said, subclassing DefaultTableCellRenderer won't do if you want to display a check box, because DTCR is a JLabel but you need to return a JCheckBox as cell renderer component. To get you startet, here is a simple demo for a custom JCheckBox based renderer:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableCellRenderer;
    
    /**
     * Demonstrates how to create a custom boolean table cell renderer.
     */
    public class BooleanRendererDemo {
    
      /**
       * Creates and displays a sample {@link JTable} with a customized boolean
       * table cell renderer.
       */
      private void start() {
        // create a table with sample data
        final JTable jt = new JTable(new MyTableModel());
    
        // configure the renderer for boolean values
        jt.setDefaultRenderer(Boolean.class, new MyBooleanRenderer());
    
    
        // boiler plate code necessary for displaying a Swing GUI 
        final JScrollPane jsp = new JScrollPane(jt);
        jsp.setPreferredSize(new Dimension(480, 320));
    
        final JFrame frame = new JFrame(BooleanRendererDemo.class.getName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(jsp);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main( String[] args ) {
        EventQueue.invokeLater(new Runnable() {
          public void run() {
            (new BooleanRendererDemo()).start();
          }
        });
      }
    
    
      /**
       * A customized table cell renderer for boolean values.
       */
      private static final class MyBooleanRenderer implements TableCellRenderer {
        private final JCheckBox jcb;
    
        MyBooleanRenderer() {
          jcb = new JCheckBox();
          jcb.setHorizontalAlignment(JCheckBox.CENTER);
        }
    
        public Component getTableCellRendererComponent(
                final JTable table,
                final Object value,
                final boolean isSelected,
                final boolean hasFocus,
                final int row,
                final int column
        ) {
          final boolean state = value != null && ((Boolean) value).booleanValue();
          if (isSelected) {
            jcb.setForeground(table.getSelectionForeground());
            jcb.setBackground(table.getSelectionBackground());
          } else {
            jcb.setForeground(table.getForeground());
            // check if the renderer should display the last column
            // if so, use custom background colors depending on the cell value
            final boolean last = table.getModel().getColumnCount() == column + 1;
            if (last) {
              jcb.setBackground(state ? Color.GREEN : Color.RED);
            } else {
              jcb.setBackground(table.getBackground());
            }
          }
          jcb.setSelected(state);
          return jcb;
        }
      }
    
      /**
       * Dummy table model with nonsense sample data.
       */
      private static final class MyTableModel extends AbstractTableModel {
        public Class getColumnClass( final int columnIndex ) {
          return columnIndex < 2 ? String.class : Boolean.class;
        }
    
        public int getColumnCount() {
          return 4;
        }
    
        public int getRowCount() {
          return 4;
        }
    
        public Object getValueAt( final int rowIndex, final int columnIndex ) {
          if (columnIndex == 3) {
            return (rowIndex % 2) == 1 ? Boolean.TRUE : Boolean.FALSE;
          } else if (columnIndex == 2) {
            return (rowIndex % 2) == 0 ? Boolean.TRUE : Boolean.FALSE;
          } else {
            return "Dummy";
          }
        }
      }
    }
    To customize cell editors, work through the JTable tutorial (and maybe take a quick look at DefaultCellEditor's OpenJDK sources).
  • 2. Re: Checkbox selected : row not rendering
    rukbat Guru Moderator
    Currently Being Moderated
    Mod: Post moved from the Java Programming forum to the Swing forum.
    If you place your posts to the correct forum, you will get speedier intelligent replies.
    You were lucky this time with regard to the first reply.
  • 3. Re: Checkbox selected : row not rendering
    Tolls Journeyer
    Currently Being Moderated
    Crossposted:
    http://www.java-forums.org/advanced-java/71585-checkbox-selected-row-not-rendering.html
  • 4. Re: Checkbox selected : row not rendering
    PhHein Guru Moderator
    Currently Being Moderated
    Mod: I've removed your other two threads about the same problem. Don't multipost!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points