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

    Default How to run swing worker in while loop ?

    Hi,
    I am able to execute the program once with inputs : s1 4.55 2123-22-22 ack. And values successively to jtable.

    I want to run the program continiously so that more values can b added and displayed. how is it possible

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.Statement;
    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 Editingser 
    { 
    	   static JTable table =  new JTable();
    	   static DefaultTableModel tableModel = new DefaultTableModel();
    	
    	   
    	   public static void main(String[] args) throws Exception {
    	        EventQueue.invokeLater(new Runnable() {
    	            @Override
    	            public void run() 
    	            {
    	                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);
    	    	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() 
    	   {	  
    		   Swingwork mainWorker = new Swingwork(table);
    		   mainWorker.execute(); 
    	
          }
    }
    
    
    class Swingwork extends SwingWorker<TableModel, TableModel>
    {
    	Scanner sc=new Scanner(System.in);
    	Object data[][]=null;
        private final JTable table;
        int i,r;
        String p;
        
        public Swingwork(JTable table) {
            this.table = table;
        }    
        @Override
        protected TableModel doInBackground() throws Exception 
        {
           	    System.out.println("Write message here: ");									
    			String 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(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
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: How to run swing worker in while loop ?

    Loop until the user enters something that identifies that they've finished (say a blank line).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: How to run swing worker in while loop ?

    Quote Originally Posted by Tolls View Post
    Loop until the user enters something that identifies that they've finished (say a blank line).
    where should i implement that ?? In which part of code and how ??

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: How to run swing worker in while loop ?

    Inside your goInBackground you are reading the input from the user, so I guess it would go in there.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. sample for jtable updation in worker thread ?
    By dinu in forum AWT / Swing
    Replies: 2
    Last Post: 04-26-2013, 04:24 PM
  2. Urgent Worker Needed!
    By William John in forum Jobs Offered
    Replies: 1
    Last Post: 04-11-2012, 04:23 PM
  3. Loop Failure with Swing.
    By Lethedethius in forum AWT / Swing
    Replies: 4
    Last Post: 02-02-2012, 10:00 AM
  4. How to loop forever in Swing-based Java app
    By GuiRitter in forum AWT / Swing
    Replies: 10
    Last Post: 04-28-2011, 10:16 AM
  5. service to worker pattern
    By philsoft in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 02:19 PM

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
  •