Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to delete rows from a JTable using swingworker?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 417
    Answer it

    Am trying to create an application that sends messages(SMS) to a list of people in the JTable and each time a message is sent to the recipient who is a row in the JTable i want that row to be deleted and i want the sending of the message to be done in the background and the JTable to be updated each time a row is deleted, i can successfully send the SMSs for the people in the JTable but the problem is on how to delete that row which is already sent from the JTable. Here is my code, kindly help:

    class QueryTable extends AbstractTableModel
     {
       Vector cache;
       int colCount;
       String[] headers;
       Object[] record;
       Connection db;
       Statement statement;
       String currentURL;
       String StudentId, StudentName;
       int avalue;
       String updateSql, s, sql, receive, insert, equals, where, delete, sqlupdate, treeString, programme, sqlc;
    
       public QueryTable()
       {
         cache = new Vector();
         try
         {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
         }
    
         catch(java.lang.Exception e)
         {
           System.err.println("Class not found exception : ");
           System.err.println(e.getMessage());
         }
       }
    
       public String getColumnName(int i) 
       {
         return headers[i];
       }
    
       public int getColumnCount() 
       {
         return colCount;
       }
    
       public int getRowCount() 
       {
         return cache.size();
       }
    
       public Object getValueAt(int row,int col)
       {
         return ((Object[])cache.elementAt(row))[col];
       }
    
       //public Class<?> getColumnClass(int col) 
       //{
         //if (col == 3 || col == 4)
         //return Boolean.class;
         //return super.getColumnClass(col);
       //}
    
       public boolean isCellEditable(int row, int col)
       { 
         return (col==3); 
       }
    
       public void setValueAt(Object value, int row, int col) 
       { 
         ((Object[])cache.elementAt(row))[col] = (Object)value;
         fireTableCellUpdated(row, col);
       }
    
       public void removeRow(int row)
       {
         cache.removeElementAt(row);
         //System.out.println("Row: " + row + " deleted");
       }
    
       public void setQuery(String q)
       {
         String DATABASE_URL = "jdbc:mysql://localhost/ndiveshca";
    
         Connection con = null;
         Statement stm = null;
         ResultSet rs = null;
    
         cache= new Vector();
         try 
         {
           con = DriverManager.getConnection(DATABASE_URL, "root", "ChIcHi_13");
    
           stm = con.createStatement();  
           rs=stm.executeQuery(q);
    
           ResultSetMetaData meta = rs.getMetaData();
           colCount = meta.getColumnCount();
           headers = new String[colCount];
    
           for (int h=1;h<=colCount;h++)
           {
             headers[h-1]=meta.getColumnName(h);
           }
    
           while(rs.next())
           {
             record = new Object[colCount];
             for(int i=0;i<colCount;i++)
             {  
               record[i]=rs.getObject(i+1);
             }
             cache.addElement(record);
           } 
    
           fireTableChanged(null);
         }
    
         catch(Exception e)
         {
           cache=new Vector();
           e.printStackTrace();
         }
       } 
     }
    
     public void runCalc() 
     {
       //progressBar.setIndeterminate(true);
       TwoWorker task = new TwoWorker(qtm);
       task.addPropertyChangeListener(new PropertyChangeListener() 
       {
         @Override
         public void propertyChange(PropertyChangeEvent e) 
         {
           if ("progress".equals(e.getPropertyName())) 
           {
             progressBar.setIndeterminate(false);
             progressBar.setValue((Integer) e.getNewValue());
           }
         }
       });
    
       task.execute();
     }
    
     private class TwoWorker extends SwingWorker<QueryTable, Integer> 
     {
       SendSMS sendSms = new SendSMS();
       QueryTable qtmo;
    
       public TwoWorker(QueryTable qtmo) 
       {
         this.qtmo = qtmo;
       }
    
       @Override
       protected QueryTable doInBackground() throws Exception 
       {
          setProgress(0);
    
          for (int Rows = 0; Rows < tm.getRowCount(); Rows++)
          {
            firstColumn = (String) table.getValueAt(Rows, 0);
            secondColumn = (String) table.getValueAt(Rows, 1);
            thirdColumn = (String) table.getValueAt(Rows, 2);
            fourthColumn = (String) table.getValueAt(Rows, 3);
    
            sendSms.sendSMStoMobile(firstColumn, secondColumn);
            //System.out.println(("Row: " + Rows) + " " + firstColumn + " : " + secondColumn);
            //qtm.removeRow(Rows);
            //table.addNotify();
            setProgress((Rows + 1) * 100 / tm.getRowCount()); //update progessbar
            publish((Rows + 1) * 100 / tm.getRowCount()); //update label
            Thread.sleep(250); // simulate latency
          }
    
          //return (0);
          return qtmo;
        }
    
        /***@Override
        protected void process(List<Integer> chunks) 
        {
          for (int d : chunks) 
          {
            //label.setText(d + "%");
            qtmo.removeRow(chunks);
            table.addNotify();
          }
        }*/
    
        @Override
        protected void process(java.util.List<Integer> list) 
        {
          for (int i = 0; i < list.size(); i++) 
          {
            //label.setText(d + "%");
            qtmo.removeRow(i);
            table.addNotify();
            //table.repaint();
          }
        }
    
        @Override
        protected void done() 
        {
          //qtm.removeAll();
          //table.addNotify();
        }
      }
      // class for sending sms
      public class SendSMS extends Thread 
      {
        OutboundNotification outboundNotification;
        StringBuffer sql1;  
        SerialModemGateway gateway;
        String smsGatewayStatus = "";
        GatewayStatuses status;
        int i=0;
        public SendSMS() 
        {
          try 
          { 
            outboundNotification = new OutboundNotification();
            SerialModemGateway gateway = new SerialModemGateway("modem.COM22", "COM22", 9600, "", "");
            gateway.setInbound(true);
            gateway.setOutbound(true);
                 Service.getInstance().setOutboundMessageNotification(outboundNotification);
            Service.getInstance().addGateway(gateway);
            Service.getInstance().startService();
            status = gateway.getStatus();
            smsGatewayStatus = status.toString();
          } 
    
          catch (Exception e) 
          {
            System.out.println("EXCEPTION gateway.getStatus : "+gateway.getStatus());
            status = gateway.getStatus();
            smsGatewayStatus = status.toString();
            e.printStackTrace();
            System.out.println("Exception e.getMessage: " + e.getMessage());
            System.out.println("Exception cause: " + e.getCause());
          }
        }
    
        public void sendSMStoMobile(String mobileNumber, String msg) throws Exception 
        {
          OutboundMessage sms = new OutboundMessage(mobileNumber, msg);
          Service.getInstance().sendMessage(sms);
        }
    
        public class OutboundNotification implements IOutboundMessageNotification 
        {
          public void process(AGateway gateway, OutboundMessage msg)
          {
            System.out.println(msg);
          }
        }
      }
    }

     

 1 Answer(s)

  • You forget to call fireTableRowsDeleted so your data is removed but the model don't alert the view. I suggest this :

       public void removeRow(int row)    {
     cache.removeElementAt(row);
     //System.out.println("Row: " + row + " deleted");    
      fireTableRowsDeleted(row, row);
     }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: