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

Thread: Flickering problem in checkbox

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

    Default Flickering problem in checkbox

    I generate a JTable dynamically with sql data. First iteratioin checkbox gets selcted and corresponding row values are fetched. In next iterations checkbox selected flicker and the correspaonding values are not selected. Can u help resolve this problem ??

    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
        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;
        
        public Clientthread(String area1,String ipp,String name)
        {
           this.area=area1;
        	ip=ipp;
        	Name=name; 
        }
    
    		public Clientthread(Object abc) 
    			{
    			Checkedvalues=abc;
    			checkedval=(String)Checkedvalues;
    			System.out.println("const : "+checkedval);
    			}
    
    			@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();
                                     
                               	  			System.out.println("Connection Established..... ");
                               	  			soundFile = new File("C:/Users/icHaNge/Downloads/cen.wav");                          	  			                     	  			
                               	  			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);
                               	  				model.fireTableDataChanged();
                               	  			   
                               	  		                             	  				
                               	  		   	     	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.repaint();
                       table.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
                    
                       table.
                          // Rendering for checkbox "select All"
                         tc = table.getColumnModel().getColumn(4);
                          tc.setHeaderRenderer(new CustomTableCellRenderer1(new MyItemListener(),Name));
                              
                          table.getCellSelectionEnabled();
                          table.setIgnoreRepaint(true);
                          table.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
                          
                         table.setDefaultRenderer(Object.class, new CustomTableCellRenderer3());
    	            
    		                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);
    			         }
    			      }
    			  }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,969
    Rep Power
    8

    Default Re: Flickering problem in checkbox

    All gui changes need to be done on the EDT. Recommended reading: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Flickering problem in checkbox

    Quote Originally Posted by KevinWorkman View Post
    All gui changes need to be done on the EDT. Recommended reading: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    I am including images and code... that will help u understand better.

    BELOW

    Flickering problem in checkbox-1.jpg
    Flickering problem in checkbox-2.jpg
    Flickering problem in checkbox-3.jpg
    Flickering problem in checkbox-5.jpg
    Flickering problem in checkbox-4.jpg

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

    Default Re: Flickering problem in checkbox

    Application images are added above for better understanding .. . .

    Input for 1st iteration :s1 4.55 2013-04-24 17:26:48.0 ACK s1 4.55 2013-04-24 21:36:07.0 nil

    Input for 2nd iteration :s1 4.55 2013-04-24 17:26:48.0 ACK s1 4.55 2013-04-24 21:36:07.0 nil s1 4.55 2013-04-24 17:26:48.0 ACK s1 4.55 2013-04-24 21:36:07.0 nil

    space is "TAB"

    Java Code:
    ....................................filter class
    import java.awt.Color;
    import java.awt.image.BufferStrategy;
    import java.util.Scanner;
     
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
     
     
    public class flicker extends JFrame implements Runnable {
     
    	JTable table;             // Creating a table
    	DefaultTableModel model = new DefaultTableModel();  // 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;  
     
        BufferStrategy myStrategy;
     
        String p=null;
        String w=null;
        // Creating an instance of class "Clientack"
        // Creating an instance to play "Sound File"
    	String area=null;
     
        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;
     
        public static void main(String [] args)
    	{
        	  flicker myRunnable = new flicker();
              Thread t = new Thread(myRunnable);
              t.start();
     }
     
     
    			@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();    
     
                                         Scanner sc = new Scanner(System.in);
                               	  			while (true)
                               	  			{                             
                               	  				System.out.println("Write message here: ");
                               	  				message=sc.nextLine();
                               	  				System.out.println("Message is: "+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;
                                          	                }
                                          	            }
                                          	        };    
     
     
                   table.setDefaultRenderer(Object.class, new CustomTableCellRenderer3());
     
                      table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
                     table.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
     
     
    		                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);                        
     
                            setVisible(true);
    				}
    				else
    				{
    					System.out.println("NO TABLE");    
    				}                    	                                         
    			}
            }                
            catch(Exception e)
               { 
                 System.out.println("Exception Occured:" +e); 
                    }   
                  }                
     
    			  private Object abc(String p)
    			     {		
    				   String val=null;
    	  				if(p.equals("ACK"))
    	  					 {
                               val="true";
    	  					  }
    	  				 else if(p.equals("nil"))
    	  					   {
    	  					     val="false";
    	  					    }
    				  return val;
    			       }			
     
    }
     
     
    ....................................customtablecellrenderer class
     
     
     
    import java.awt.Component;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import javax.swing.AbstractButton;
    import javax.swing.DefaultCellEditor;
    import javax.swing.JCheckBox;
    import javax.swing.JLabel;
    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;
    Object abc="null";
    private String Name;
     
    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) 
    {
        this.row = row;
        this.table1=table;
        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);
          String Value =(String) table1.getValueAt(row, 1);
    	  String Date =(String) table1.getValueAt(row, 2);
    	  //String Status=(String) table1.getValueAt(row, 3);
    	  Boolean select=(Boolean) table1.getValueAt(row,4);
          if (Boolean.TRUE.equals(select)) 
           {
    	    abc += Sensor+"\t"+Value+"\t"+Name+"\t"+Date+"\t";
    	    System.out.println(abc);
        }
        else
        {
        }
        }
     
       }
     
     
    ....................................customtablecellrenderer3 class
     
     
     
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
     
     
    public class CustomTableCellRenderer3 extends DefaultTableCellRenderer{
     
    	  public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    	  {
     
        Component c = super.getTableCellRendererComponent(table, value, isSelected, isSelected, 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;
      }
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Flickering problem in checkbox

    You hace made your JFrame implement Runnable.
    You are then cerating the JFrame in its own thread, which is *not* the EDT.
    This is completely wrong.

    First, there is rarely ever a need to extend JFrame, and I can't see one here.
    Second you don't create your GUI on any old random thread, you need to build it on the EDT.
    That means using the util method invokeLater().
    Long running non-GUI code, if launched in the EDT (eg from a button click) should be launched in a SwingWorker, to get it off the EDT.

    All this is in the tutorial link provided above which you are going to have to go through and correct your code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Flickering problem in checkbox

    Quote Originally Posted by Tolls View Post
    You hace made your JFrame implement Runnable.
    You are then cerating the JFrame in its own thread, which is *not* the EDT.
    This is completely wrong.

    First, there is rarely ever a need to extend JFrame, and I can't see one here.
    Second you don't create your GUI on any old random thread, you need to build it on the EDT.
    That means using the util method invokeLater().
    Long running non-GUI code, if launched in the EDT (eg from a button click) should be launched in a SwingWorker, to get it off the EDT.

    All this is in the tutorial link provided above which you are going to have to go through and correct your code.
    where is the link ????

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Flickering problem in checkbox

    Look at post #2.
    Kevin provided a link to the tutorials.
    You even replied to it.
    dinu likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. problem with checkbox in struts2
    By sunithamm in forum Web Frameworks
    Replies: 8
    Last Post: 03-15-2011, 11:56 AM
  2. Struts- checkBox default set problem
    By apoorv.rastogi in forum Web Frameworks
    Replies: 0
    Last Post: 10-25-2010, 11:51 AM
  3. Replies: 3
    Last Post: 07-27-2010, 09:52 AM
  4. flickering problem
    By dimril in forum Java 2D
    Replies: 5
    Last Post: 06-11-2010, 08:52 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
  •