Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Jtble not updating using swing worker thread ?

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Jtble not updating using swing worker thread ?

    I want to update the Jtable when a message is entered. The Jtable does not show any values . I also want to update the Jtable when new values are entered ?
    below is the code and snap shot of Image
    1.jpg

    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.SwingWorker;
    import javax.swing.table.DefaultTableModel;
     
     
    public class flicker extends JFrame {
    	JTable table;            
    	DefaultTableModel model = new DefaultTableModel();  
        JButton Acknowledge;      
        JPanel Panel=new JPanel();           
        JScrollPane Pane;       
        JScrollPane Pane1;      
        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;
    	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;
     
        public flicker(String string) {
    		super(string);
     
    		  Panel.setLayout(null);
    	      getContentPane().add(Panel);    
     
              start();
     
              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);       
     
         	  setSize(520,570);
       	      setDefaultCloseOperation(DISPOSE_ON_CLOSE);      
              setVisible(true);	
    	}
     
        private void start() 
        {
         try
            {  
                                 final Scanner sc = new Scanner(System.in);
                                 SwingWorker<Object, Object> worker = new SwingWorker<Object, Object>() 
                                	 {
                                         @Override
                                	    protected Object doInBackground()
                                	    {       
                                        	 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;
                                       	                    }
                                       	               }    
                                       	            };    
                                       	         publish(table);
                                	          }                       	  
                            	  				else
                            	  				{
                            	  					System.out.println("NO TABLE");    
                            	  				}                    	                                         
                            	  				return table;
                                	    }
     
                                 protected void process(JTable table)
                                 {                                                       	        
                       //   table.setDefaultRenderer(Object.class, new CustomTableCellRenderer3());
     
                         //       table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
                       //  table.getColumnModel().getColumn(4).setCellEditor(new CustomTableCellRenderer(new JCheckBox(),Name));
     
                             model.fireTableDataChanged();
                             table.repaint();
                                  }                         
                                 };
                       worker.execute();
     
     
            }
         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;
    	       }
     
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Jtble not updating using swing worker thread ?


  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Jtble not updating using swing worker thread ?

    Duplicate of http://www.javaprogrammingforums.com...ox-jtable.html
    Please read the forum rules. Thread locked.

Similar Threads

  1. example for updation of JTABLE using swing worker thread
    By harshilshah in forum AWT / Java Swing
    Replies: 2
    Last Post: April 26th, 2013, 11:45 AM
  2. Thread dysfunctioning in swing application
    By rpathak123 in forum Threads
    Replies: 0
    Last Post: April 4th, 2013, 04:19 AM
  3. [SOLVED] Worker threads in Swing
    By angstrem in forum Threads
    Replies: 8
    Last Post: March 16th, 2013, 04:01 PM
  4. updating EDT with thread swingworker/invokeLater ?
    By mdstrauss in forum AWT / Java Swing
    Replies: 0
    Last Post: October 11th, 2009, 04:52 AM
  5. Replies: 0
    Last Post: October 10th, 2009, 01:25 PM

Tags for this Thread