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: SwingWorker updating JTABLE ...

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

    Default SwingWorker updating JTABLE ...

    Guys,

    I want to update the JTABLE with contents of database using swing worker . I have a code below,but it doesnt show the data in table ?
    There are no errors, please help me with rewriting the code.
    snapshot included..1.jpg

     
    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.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 Ser 
    { 
    	   static JTable table =  new JTable();
    	   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(model);
    	    	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.setVisible(true);
    	    }
     
    	   private static void execTask() {	  
    		   Swingworke mainWorker = new Swingworke(table);
    		   mainWorker.execute(); 
     
          }
    }
     
    class Swingworke extends SwingWorker<TableModel, TableModel> {
     
        private final JTable table;
     
        public Swingworke(JTable table) {
            this.table = table;
        }    
        @Override
        protected TableModel doInBackground() throws Exception {
            Vector data = new Vector();
            Vector columns = new Vector();
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {              	
            	Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/moxa?zeroDateTimeBehavior=convertToNull", "root", "root");
                String sql = "Select * from alarmval1";
                Statement statement = con.createStatement();
                ResultSet resultSet = statement.executeQuery(sql);
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columnCount = metaData.getColumnCount();
                //store column names  
                for (int i = 1; i <= columnCount; i++) {
                    columns.add(metaData.getColumnName(i));
                }
     
                columns.ensureCapacity(columnCount);
     
                Vector row;
                while (resultSet.next()) {
     
                    row = new Vector(columnCount);
                    for (int i = 1; i <= columnCount; i++) {
                        row.add(resultSet.getString(i));
                        System.out.println(resultSet.getString(i));
                    }
                    data.add(row);
     
                    //Debugging                
                }
     
                // List.setModel(tableModel);
     
            } finally {
                try {
                    ps.close();
                } catch (Exception e) {
                }
                try {
                    rs.close();
                } catch (Exception e) {
                }
            }
     
            DefaultTableModel tableModel = new DefaultTableModel(data, columns);
            return tableModel;
        }
     
        @Override
        protected void done() {
            try {
                TableModel model = get();
                table.setModel(model);
            } catch (InterruptedException | ExecutionException ex) {
                ex.printStackTrace();
            }
        }
    }

    Thanks...
    Last edited by harshilshah; April 30th, 2013 at 05:38 AM. Reason: change of code..


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: SwingWorker updating JTABLE ...

    Try to follow the flow of the program by using println to log what is happening. Try to determine where things do not happen as you expect. Include as many details with your question as you can.

    "but it doesnt show the data in table ?" ...is not really a great question about the problem you have.

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

    Default Re: SwingWorker updating JTABLE ...

    Quote Originally Posted by jps View Post
    Try to follow the flow of the program by using println to log what is happening. Try to determine where things do not happen as you expect. Include as many details with your question as you can.

    "but it doesnt show the data in table ?" ...is not really a great question about the problem you have.
    FOUND THE SOLUTION ....

Similar Threads

  1. [SOLVED] New to SwingWorker how to use process?
    By Niteawk in forum Threads
    Replies: 3
    Last Post: December 5th, 2012, 10:17 AM
  2. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  3. Problem with updating empty JTable
    By byubi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 15th, 2010, 02:06 AM
  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