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 7 of 7

Thread: while looping a swing worker does not display results on jtable?

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

    Default while looping a swing worker does not display results on jtable?

    hi,
    I am using swing worker to update my GUI in edt. I have the code below. when I run the code without the while loop it works,
    whereas when I run with the while loop it falis , why is it so ?

     
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Vector;
    import java.util.concurrent.ExecutionException;
     
    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;
    import javax.swing.table.TableModel;
     
    public class App 
    { 	 
    	     static JFrame frame = new JFrame("Random Data Demo");
    	     static JTable table =  new JTable();
    	     static DefaultTableModel tableModel = new DefaultTableModel();
    	 	 static Scanner sc=new Scanner(System.in);
     
    	   public static void main(String[] args) throws Exception {
    	        EventQueue.invokeLater(new Runnable() {
    	            @Override
    	            public void run() 
    	            {            	
    	               while(true)                                                     ////// while loop ---
    	               {
     
    	            	System.out.println("Write message here: ");									
    	     			String message = sc.nextLine();
    	     			System.out.println("Message is: "+message);
     
    	            	Swingwor mainWorker = new Swingwor(table,message);
    	                mainWorker.execute(); 
     
    	                init();
     
    	               }
    	            }
    	        });
    	    }
     
    	   private static void init() {
    		   //execTask();
    		 // JFrame frame = new JFrame("Random Data Demo");
    	       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	      frame.setSize(600,600);
     
    	    //	DefaultTableModel model = new DefaultTableModel();
    	    	//JTable table =  new JTable(model);
    	    	table.setModel(tableModel);
     
    	    	table.repaint();
    	    	JScrollPane scrollPane = new JScrollPane(table);
     
    	    	JPanel panel = new JPanel(new BorderLayout());
    	    	//Swingworker mainWorker = new Swingworker(table);
               // mainWorker.execute();
     
    	    	panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
    	    	panel.add(scrollPane, BorderLayout.CENTER);
    	    	//getContentPane().add(panel);
                  frame.add(panel);
    	          frame.setVisible(true);
    	    }
     
    	 //  private static void execTask() 
    	  // {	  
    		//   Swingwor mainWorker = new Swingwor(table);
    	//	   mainWorker.execute(); 
     
       //   }
    }
     
     
    class Swingwor extends SwingWorker<TableModel,TableModel>
    {
     
    	Object data[][]=null;
        private final JTable table;
        int i,r;
        String p;
        String message=null;
        Object col[] = {"Sensor","Values","Arrival_Time","Status",""};
     
        public Swingwor(JTable table, String message) {
            this.table = table;
            this.message = message;
        }    
        @Override
        protected TableModel doInBackground() throws Exception 
        {     	   	
    			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(p) ;
    				  System.out.println(data[r][4]);
    				}               
    				//System.out.println(java.util.Arrays.deepToString(data));
                 //Display Table     
                      }
    			DefaultTableModel tableModel = new DefaultTableModel(data, col);
    	        tableModel.setDataVector(data, col);
    			 return tableModel;
              }
     
     
        @Override
        protected void done() {
            try {
                TableModel model = get();
                table.setModel(model);
            } catch (InterruptedException | ExecutionException ex) {
                ex.printStackTrace();
            }
        }
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: while looping a swing worker does not display results on jtable?

    it falis
    Please explain what "it fails" means? What happens when the code executes?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: while looping a swing worker does not display results on jtable?

    fails means : IT dosn't show the values in the jtable... The program executes without errors. input is also given,but the input is not shown in jtable

    --- Update ---

    u can try executing the program ..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: while looping a swing worker does not display results on jtable?

    Why is setModel() called in 2 places? One call is with an empty model.

    Why the loop in run() that is creating panel each time around the loop?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: while looping a swing worker does not display results on jtable?

    Quote Originally Posted by Norm View Post
    Why is setModel() called in 2 places? One call is with an empty model.

    Why the loop in run() that is creating panel each time around the loop?
    oh !! I again modified the code,solving the above problem.. But still no result ??

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.util.List;
    import java.util.Scanner;
    import java.util.Vector;
    import java.util.concurrent.ExecutionException;
     
    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;
    import javax.swing.table.TableModel;
     
    public class App 
    { 	 
    	     static JFrame frame = new JFrame("Random Data Demo");
    	     static JTable table =  new JTable();
    	 	static JPanel panel = new JPanel(new BorderLayout());
    	     static DefaultTableModel tableModel = new DefaultTableModel();
    	 	 static Scanner sc=new Scanner(System.in);
     
    	   public static void main(String[] args) throws Exception {
    	           EventQueue.invokeLater(new Runnable() {
    	            @Override
    	            public void run() 
    	            {     
    	              while(true)
    	              {
    	            	Swingwor mainWorker = new Swingwor(table);
    	                mainWorker.execute(); 
     
     
    	                init();
    	                try {
    						Thread.sleep(10000);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    	              }
    	            	}            
    	        });
    	    }
     
    	   private static void init() {
    		   //execTask();
    		 // JFrame frame = new JFrame("Random Data Demo");
    	       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	      frame.setSize(600,600);
     
    	    //	DefaultTableModel model = new DefaultTableModel();
    	    	//JTable table =  new JTable(model);
    	    	//table.setModel(tableModel);
     
    	    	table.repaint();
    	    	JScrollPane scrollPane = new JScrollPane(table);
     
     
    	    	//Swingworker mainWorker = new Swingworker(table);
               // mainWorker.execute();
     
    	    	panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
    	    	panel.add(scrollPane, BorderLayout.CENTER);
    	    	//getContentPane().add(panel);
                  frame.add(panel);
    	          frame.setVisible(true);
    	    }
     
    	 //  private static void execTask() 
    	  // {	  
    		//   Swingwor mainWorker = new Swingwor(table);
    	//	   mainWorker.execute(); 
     
       //   }
    }
     
     
    class Swingwor extends SwingWorker<TableModel,TableModel>
    {
     
    	Object data[][]=null;
        private final JTable table;
        int i,r;
        String p;
    	 static Scanner sc=new Scanner(System.in);
        String message=null;
        Object col[] = {"Sensor","Values","Arrival_Time","Status",""};
     
        public Swingwor(JTable table) {
            this.table = table;
         //   this.message = message;
        }    
        @Override
        protected TableModel doInBackground() throws Exception 
        {     	
        	    System.out.println("Write message here: ");									
    			String message = sc.nextLine();
    			System.out.println("Message is: "+message);
     
    			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(p) ;
    				  System.out.println(data[r][4]);
    				}               
    				//System.out.println(java.util.Arrays.deepToString(data));
                 //Display Table     
                      }
     
    			DefaultTableModel tableModel = new DefaultTableModel(data, col);	
    	        tableModel.setDataVector(data, col);
    	        publish(tableModel);
     
    			 return tableModel;
        }
     
     
        @Override
        protected void process(List<TableModel> chunks)
        {
        	TableModel model = null;
    		try {
    			model = get();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (ExecutionException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            table.setModel(model);
        }
     
        @Override
        protected void done() {
            try {
                TableModel model = get();
                table.setModel(model);          
            } catch (InterruptedException | ExecutionException ex) {
                ex.printStackTrace();
            }
        }
     
     
     
    }


    --- Update ---

    norm : is it something that the loop is busy executing the job,thats y its not able to update the GUI ??

  6. #6

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: while looping a swing worker does not display results on jtable?

    loop is busy executing the job,thats y its not able to update the GUI
    If the loop is executing using the EDT's thread, the GUI won't be updated until the code using that thread returns it to the JVM.

    To see what thread is being used add a println statement to print out the value of Thread.currentThread() in all the methods being called.
    If you don't understand my answer, don't ignore it, ask a question.

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. Jtble not updating using swing worker thread ?
    By harshilshah in forum AWT / Java Swing
    Replies: 2
    Last Post: April 26th, 2013, 09:10 AM
  3. [SOLVED] Worker threads in Swing
    By angstrem in forum Threads
    Replies: 8
    Last Post: March 16th, 2013, 04:01 PM
  4. How do i display each loop results
    By Amshank24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 14th, 2013, 07:43 AM
  5. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM

Tags for this Thread