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

    Default Java Thread : Checkbox bug in JTABLE .

    Hi,

    I have a JTable populating values from database and checkboxes for every value.
    The program runs properly for 1st time .ie when a checkbox is checked it takes the corresponding row values.

    Once new values are added to database. the table gets populated with new values. but when the checkbox are checked again they no
    longer fetch the corresponding row values. The checkbox keep flickering in the table. please help me with this bug.

    Java Code:
    public class Clientthread extends JFrame implements Runnable 
    { 
    	private static final long serialVersionUID = 1L;
    	JTable table;             // Creating a table
    	DefaultTableModel model;  // Defining a model for the table	
    
        int PORT = 8134;
        Socket sock=null;
        InputStream inStream = null;
        DataInputStream inDataStream;  
        OutputStream outStream ;
    	DataOutputStream outDataStream ;
    	
        int i,r;   
        Object data[][]=null;
    
    			@Override
                public void run() 
                {
                	 try
                     {                   
                                	 setTitle( group );
                                	 setSize(520,570);
                                	 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                                	               
                                     Panel=new JPanel();
                                     Panel.setLayout(null);
                                     getContentPane().add(Panel);                              
                                     
                                      model = new DefaultTableModel();
                                     
                               	  			System.out.println("Connection Established..... ");
                               	  			while (true)
                               	  			{                
                               	  				Object col[] = {"Sensor","Values","Arrival_Time","Status",""};
                               	  				if(!message.equals("ITS EMPTY"))
                               	  				{
                               	  					String[] parts = message.split("\t");
                               	  				    data = new Object[parts.length / 4][5];
                               	  					for (i = 0, r = 0; r < data.length; r++) 
                               	  					{
                           	  						data[r][0] =  parts[i++];
                           	  					   //data[r][1] =  new Double(parts[i++]) ;
                           	  					    data[r][1] =  parts[i++] ;
                           	  					    data[r][2] = parts[i++];
                           	  					    data[r][3] = parts[i++];
                           	  					    p=(String) data[r][3];
                           	  					    w = (String) abc(p);
                           	  					    data[r][4]=Boolean.parseBoolean(w) ;
                           	  					  // System.out.println(data[r][4]);
                               	  					}               
                               	  					//System.out.println(java.util.Arrays.deepToString(data));
                               	  					
                                                 //Display Table     
                               	  				model.setDataVector(data, col);
                               	  		                             	  				
                               	  		   	     	table = new JTable(model)
                                          	        {
                                          	            private static final long serialVersionUID = 1L;
                                          	            @Override
                                          	            public Class getColumnClass(int column) {
                                          	                switch (column) {
                                          	                    case 0:
                                          	                        return String.class;
                                          	                    case 1:
                                          	                        return String.class;
                                          	                    case 2:
                                        	                        return String.class;
                                          	                    case 3:
                                      	                            return String.class;
                                          	                    default:
                                          	                        	return Boolean.class;
                                          	                }
                                          	            }
                                          	        };    
                              
    
                          table.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
                    
                          // Rendering for checkbox "select All"
                            tc = table.getColumnModel().getColumn(4);
                           tc.setHeaderRenderer(new CustomTableCellRenderer1(new MyItemListener(),Name));
                              
                          
                             table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer()    
                          {
                             @Override
                           public Component getTableCellRendererComponent(JTable table,
                                   Object value, boolean isSelected, boolean hasFocus, int row,int column) 
                             {
                                Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
                                    Boolean last=(Boolean)table.getValueAt(row, 4);       
                                    String status=(String)table.getValueAt(row, 3);  
                                    if(Boolean.TRUE.equals(last) && status.equals("ACK"))
                                	    {
                                         c.setBackground(Color.green);
                                        }
                                    else if(Boolean.TRUE.equals(last) && status.equals("nil"))
                             	    {
                                      c.setBackground(Color.yellow);
                                     }
                                    else
                                       {
                                   c.setBackground(Color.red);
                                       }
                                    table.repaint();
                                return c;
                               
                             }
                          });
    		               
    		                Pane=new JScrollPane(table);
                            Pane.setSize(500,500);
                            
                            Pane1=new JScrollPane(Acknowledge);
                            Pane1.setSize(90,30);
                            Pane1.setBounds(0,500,320,30);
                            
                            Pane2=new JScrollPane(Options);
                            Pane2.setBounds(320,500,180,30);
                            
                            
    		                Panel.add(Pane);       
                            Panel.add(Pane1);
                            Panel.add(Pane2);
                            
                            Acknowledge.setVisible(true);
                            Acknowledge.setEnabled(true);
                            
                            setVisible(true);
    				}                   	                                         
    			}
            }                
            catch(Exception e)
               { 
                 System.out.println("Exception Occured:" +e); 
                    }   
                  }                
                 catch(UnknownHostException ex)
                   { 
                     Logger.getLogger(Clientthread.class.getName()).log(Level.SEVERE, null, ex); 
                     }
                  catch (IOException ex) 
                  {
                    Logger.getLogger(Clientthread.class.getName()).log(Level.SEVERE, null, ex);
                    }   
                 }
    
                   
    			  private Object abc(String p)
    			     {		
    				   String val=null;
    	  				if(p.equals("ACK"))
    	  					 {
                               val="true";
    	  					  }
    	  				 else if(p.equals("nil"))
    	  					   {
    	  					     val="false";
    	  					    }
    				  return val;
    			       }
    
    
    			class MyItemListener implements ItemListener
    			  {
    			    public void itemStateChanged(ItemEvent e) {
    			      Object source = e.getSource();
    			      if (source instanceof AbstractButton == false) return;
    			      boolean checked=e.getStateChange() == ItemEvent.SELECTED;
    			      System.out.println(checked);
    			      for(int x = 0, y = table.getRowCount(); x < y; x++)
    			      {
    			        table.setValueAt(new Boolean(checked),x,3);
    			      }
    			      if(checked==true)
    			      {
    			    	  int row=table.getRowCount();
    			          if(row!=0)
    			            {
    			             for(int i=0;i<=row-1;i++)
    			             {
    			  	           String Sensor =(String) table.getValueAt(i, 0);
    			  	           String Value =(String) table.getValueAt(i, 1);
    			  	           String Date =(String) table.getValueAt(i, 2);
    			  	           abc += Sensor+"\t"+Value+"\t"+Name+"\t"+Date+"\t";
    			  	      //  Clientthread ct=new Clientthread(abc);
    			             }
    		               }
                        }
    			      else
    			         {
    			    	      abc=null;
    			    	     // Clientthread ct=new Clientthread(abc);
    			         }
    			      }
    			  }

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

Similar Threads

  1. checkBox with jtable and mysql
    By ashim in forum New To Java
    Replies: 1
    Last Post: 09-15-2012, 01:22 PM
  2. Checkbox editor for JTable column
    By ShamPhadtale in forum AWT / Swing
    Replies: 12
    Last Post: 10-11-2011, 05:38 PM
  3. Disable a checkbox in jtable
    By pink123 in forum AWT / Swing
    Replies: 8
    Last Post: 03-24-2011, 06:29 PM
  4. CheckBox in JTable
    By nikosa in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2009, 05:01 AM
  5. checkBox in Jtable renderining problem
    By pothraj in forum AWT / Swing
    Replies: 1
    Last Post: 12-18-2008, 11:39 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
  •