Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Checkbox selected : row not rendering

  1. #1
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Checkbox selected : row not rendering

    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

    Java Code:
     table.getColumnModel().getColumn(3).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
      table.getColumnModel().getColumn(3).setCellRenderer(new CustomTableCellRenderer3());
    Java Code:
    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);
        }
        else
        {
        	//work around code;
        //	System.out.println("do nothing");
        	/*//remove already there
         Object a = ((String) abc).split("Value"); 
         a=((String) abc).split("Sensor"); 
         abc=a;
        	System.out.println(a);*/
        }
        }
    
       }
    Java Code:
    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;
                     }
    	
    }

  2. #2
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: Checkbox selected : row not rendering

    ignore the comments ...

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Checkbox selected : row not rendering

    You are missing some code like the main entry point and Clientthread. You need to provide the smallest, self-contained, compilable example that demonstrates the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: Checkbox selected : row not rendering

    Quote Originally Posted by dinu View Post
    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

    Java Code:
     table.getColumnModel().getColumn(3).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
      table.getColumnModel().getColumn(3).setCellRenderer(new CustomTableCellRenderer3());
    Java Code:
    setcellEditor:
    
    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";
        }
        }
    Java Code:
    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;
                     }
    	
    }
    I have edited the code... wat do u mean by main entry point ?

  5. #5
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: Checkbox selected : row not rendering

    Checkbox selected : row not rendering-r1.jpg

  6. #6
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: Checkbox selected : row not rendering

    Checkbox selected : row not rendering-r2.jpg

  7. #7
    dinu is offline Member
    Join Date
    Jan 2013
    Posts
    31
    Rep Power
    0

    Default Re: Checkbox selected : row not rendering

    the problem is when I select the single checkboxes the row should change the color to red.

    IF Select all is checked all the rows should change the color.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Checkbox selected : row not rendering

    Crossposted at OTN.
    PhHein likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 03-21-2012, 05:57 PM
  2. Replies: 4
    Last Post: 04-13-2011, 03:35 AM
  3. Checkbox items in combox rendering
    By charanjeet_sm in forum AWT / Swing
    Replies: 2
    Last Post: 12-13-2010, 08:10 PM
  4. Replies: 3
    Last Post: 07-27-2010, 09:52 AM
  5. How to retain the checkbox selected after page load...?
    By prabhurangan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-24-2008, 08:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •