eriezelagera

SwingWorker - Loop

Aug 18th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. // Parent class
  2.  
  3. /**
  4.  * Initialize background services.
  5.  */
  6. private void initServices() {
  7.         this.dtModel = (DefaultTableModel) tblHistory.getModel(); // Default data model of the jTable
  8.         this.updateTimer = new Timer(1000, new ActionListener() { // I don't know if this is right, I use Timer to loop the Swingworker
  9.             @Override
  10.         public void actionPerformed(ActionEvent e) {
  11.                     new CallHistoryWatcherService(CallHistory.this, updateTimer, dtModel).execute();
  12.                 }
  13.         });
  14.         updateTimer.setRepeats(false);
  15.     updateTimer.start();
  16. }
  17.  
  18. // Watcher class
  19.  
  20. /**
  21.  * Watch the CallInformations if any update has been made.
  22.  * This will check every seconds.
  23.  * @param history
  24.  * @param updateTimer
  25.  * @param dtModel
  26.  */
  27. public CallHistoryWatcherService(CallHistory history, javax.swing.Timer updateTimer, DefaultTableModel dtModel) {
  28.     // Initialize values
  29. }
  30.  
  31. @Override
  32. protected Collection<Vector> doInBackground() throws Exception {
  33.         Collection<Vector> chunks = new ArrayList<>();
  34.         for (CallInformations info : CallInformations.getCallInformations(OrderBy.Ascending)) {
  35.         Vector v = new Vector();
  36.                 // Get from data from Database then place it to Vector
  37.                 chunks.add(v);
  38.         }
  39.         return chunks;
  40. }
  41.  
  42. @Override
  43. protected void done() {
  44.         try {
  45.         history.updateTableData(get()); // From parent class, manually update the data based on new Collection of data
  46.         System.out.println("Call History refreshed...");
  47.         } catch (ExecutionException | InterruptedException e) {
  48.         e.printStackTrace();
  49.         }
  50.         COLUMN_NAMES.clear();
  51.         updateTimer.restart(); // Again, I don't know if this is right, restart the timer to execute the SwingWorker
  52. }
Advertisement
Add Comment
Please, Sign In to add comment