Skip to main content

Jcheckbox flickering problem.

No replies
dinud123
Offline
Joined: 2013-03-20
Points: 0

I populate JTable with database values. On first iteration the checkbox checked gets the corresponding JTABLE cell values. On second iteration the checkbox checked "flickers" and nolonger take the cell values? can some1 help me solve the flickering of checkboxes ?

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
    JButton Acknowledge;      // Creating an instance of Button "Acknowledge"
    JPanel Panel;             // Creating a panel to hold below 2 panes
    JScrollPane Pane;         // Creating a pane to hold the table
    JScrollPane Pane1;        // Creating a pane1 to hold the Button "Acknowledge"
    JScrollPane Pane2;
    String[] Optlist = {"Show Alarm", "Show History", "Report Alarm (PDF)", "Report History (PDF)"};
    JComboBox Options=null; 
 
    String p=null;
    String w=null;
    Clientack ru=null;        // Creating an instance of class "Clientack"
File soundFile = null;    // Creating an instance to play "Sound File"
TableColumn tc=null;
String area=null;

// Boolean last=null;

JFrame Frame1=new JFrame();
JFrame Frame2=new JFrame();

    int PORT = 8134;
    Socket sock=null;
    InputStream inStream = null;
    DataInputStream inDataStream; 
    OutputStream outStream ;
DataOutputStream outDataStream ;

    int i,r;  
    String ip,selection,stso,group,message,p1,p2,Sensor;
    static String checkedval;
    static String Name;
    Double Value;
    Object data[][]=null;
    static Object abc=null;
    static Object Checkedvalues;
   

@Override
            public void run()
            {
            try
                 {          
                             sock=new Socket(ip,PORT);
                             System.out.println("Socket Established at 8134..");
                             try
                             {            
                            setTitle( group );
                            setSize(520,570);
                            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                                          
                                 Panel=new JPanel();
                                 Panel.setLayout(null);
                                 getContentPane().add(Panel);                             
                                
                                model = new DefaultTableModel();
                                
          try
                {
         outStream = sock.getOutputStream();
         outDataStream = new DataOutputStream (outStream);
         outDataStream.writeUTF(area);
                }
                            
                catch(Exception e)
                           {
         System.out.println("Cannot Send Server Group Values");
                         }
           while (true)
               {                            
                     inStream = sock.getInputStream ();
                     inDataStream = new DataInputStream ( inStream );
                     message = inDataStream.readUTF();
                    System.out.println("Server Sent Moxa values : "+message);
                 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;
                                                      }
                                                  }
                                              };   
                                             
                        ///  TableColumn tc=table.getColumnModel().getColumn(3);
                         // table.removeColumn(tc);
                          //table.repaint();                  
             

                     
                   table.setPreferredScrollableViewportSize(table.getPreferredSize());
  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.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
                     
     table.setDefaultRenderer(Object.class, new CustomTableCellRenderer3());
Logger.getLogger(Clientthread.class.getName()).log(Level.SEVERE, null, ex);
}

                Acknowledge.setBackground(Color.green);                                
                Acknowledge.setBounds(0,560,580,25);
               
                Options.setBackground(Color.cyan);
              
                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);
                        ru = new Clientack(table);
                      //   new Thread(ru).start();
}
else
{
System.out.println("NO TABLE");   
}                                                            
}
        }               
        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,4);
      }
      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);
         }
      }
  }
       

}