By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,653 Members | 1,419 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,653 IT Pros & Developers. It's quick & easy.

Why I cannot see the updated values of JTable in Java UI?

P: 28
I have a Download Manager project and when the download starts I must see the progress,speed and other related data about downloads.When the user add a URL , I can see the new download in the JTable but not in progress, I just saw that its progress in 0% and when I click some cell in the list and then again click another cell, I saw that it changed but why I must click it.If I don't click those downoads list' cells, i cannot see the progress.I also used Obsservable class and in the Download class I wrote
Expand|Select|Wrap|Line Numbers
  1.  // Notify observers that this download's status has changed.
  2.         public void stateChanged() {
  3.             setChanged();
  4.             notifyObservers();
  5.         }
  6.  
Here is the TableModel.Its object is created in DownloadManagerView(my view class).
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import javax.swing.*;
  3. import javax.swing.table.*;
  4. import downloadmanager.controller.DownloadManagerSystem;
  5. import downloadmanager.model.Download;
  6.  
  7. // This class manages the download table's data.
  8. class DownloadsTableModel extends AbstractTableModel
  9.         implements Observer {
  10.  
  11.     // These are the names for the table's columns.
  12.     private static final String[] columnNames = {"URL", "Name",
  13.     "Size", "Progress %","Status","Time Left","Transfer Rate","Date"};
  14.  
  15.     // These are the classes for each column's values.
  16.     private static final Class[] columnClasses = {String.class,
  17.     String.class, String.class,JProgressBar.class, String.class, String.class,String.class,Date.class};
  18.  
  19.  
  20.   // Add a new download to the table.
  21.  
  22.     public void addDownloadToTable(Download download) {
  23.  
  24.         download.addObserver(this);
  25.  
  26.         // Fire table row insertion notification to table.
  27.         fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
  28.     }
  29.  
  30.     // Get a download for the specified row.
  31.     public Download getDownload(int row) {
  32.         return (Download) DownloadManagerSystem.getArrayList().get(row);
  33.     }
  34.  
  35.     // Remove a download from the list.
  36.     public void clearDownload(int row) {
  37.         DownloadManagerSystem.getArrayList().remove(row);
  38.  
  39.         // Fire table row deletion notification to table.
  40.         fireTableRowsDeleted(row, row);
  41.     }
  42.  
  43.     // Get table's column count.
  44.     public int getColumnCount() {
  45.         return columnNames.length;
  46.     }
  47.  
  48.     // Get a column's name.
  49.     public String getColumnName(int col) {
  50.         return columnNames[col];
  51.     }
  52.  
  53.     // Get a column's class.
  54.     public Class getColumnClass(int col) {
  55.         return columnClasses[col];
  56.     }
  57.  
  58.     // Get table's row count.
  59.     public int getRowCount() {
  60.  
  61.         return DownloadManagerSystem.getArrayList().size();
  62.  
  63.     }
  64.  
  65.     // Get value for a specific row and column combination.
  66.     public Object getValueAt(int row, int col) {
  67.  
  68.         Download download = (Download)DownloadManagerSystem.getArrayList().get(row);
  69.         switch (col) {
  70.             case 0: // URL
  71.                 return download.getUrl();
  72.             case 1: // Name
  73.                 return download.getName();
  74.             case 2: // Size
  75.                 int size = download.getSize();
  76.                 return (size == -1) ? "" : Integer.toString(size);
  77.  
  78.             case 3: // Progress
  79.                  return new Float(download.getProgress());
  80.  
  81.             case 4://Status
  82.                 return Download.STATUSES[download.getStatus()];
  83.             case 5: //TimeLeft
  84.                 return download.getTimeLeft();
  85.             case 6://TransferRate
  86.                 return download.getSpeed();
  87.             case 7:// Date
  88.                 return download.getDate();
  89.  
  90.  
  91.         }
  92.         return "";
  93.     }
  94.  
  95.   /* Update is called when a Download notifies its
  96.      observers of any changes */
  97.     public void update(Observable o, Object arg) {
  98.         int index = DownloadManagerSystem.getArrayList().indexOf(o);
  99.  
  100.         // Fire table row update notification to table.
  101.         fireTableRowsUpdated(index, index);
  102.     }
  103. }
The arraylist is created in model class DownloadManager.java and I call the ArrayList in the TableModel.Instead of creating two lists I used the Arraylist in the model.Is my problem related with this?
Before model I created the list and I made the adding to list process in view and It was working.But now I must do the project in the MVC structure, so I only created the list in the Model.Now, I cannot see any updated data about the downloads why is it so?I also uploaded my project zip.Please help.
Attached Files
File Type: zip DownloadManagerMVC1.zip (337.7 KB, 36 views)
Apr 27 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.