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
Here is the TableModel.Its object is created in DownloadManager View(my view class).
The arraylist is created in model class DownloadManager .java and I call the ArrayList in the TableModel.Inst ead 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.
Code:
// Notify observers that this download's status has changed.
public void stateChanged() {
setChanged();
notifyObservers();
}
Code:
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import downloadmanager.controller.DownloadManagerSystem;
import downloadmanager.model.Download;
// This class manages the download table's data.
class DownloadsTableModel extends AbstractTableModel
implements Observer {
// These are the names for the table's columns.
private static final String[] columnNames = {"URL", "Name",
"Size", "Progress %","Status","Time Left","Transfer Rate","Date"};
// These are the classes for each column's values.
private static final Class[] columnClasses = {String.class,
String.class, String.class,JProgressBar.class, String.class, String.class,String.class,Date.class};
// Add a new download to the table.
public void addDownloadToTable(Download download) {
download.addObserver(this);
// Fire table row insertion notification to table.
fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
// Get a download for the specified row.
public Download getDownload(int row) {
return (Download) DownloadManagerSystem.getArrayList().get(row);
}
// Remove a download from the list.
public void clearDownload(int row) {
DownloadManagerSystem.getArrayList().remove(row);
// Fire table row deletion notification to table.
fireTableRowsDeleted(row, row);
}
// Get table's column count.
public int getColumnCount() {
return columnNames.length;
}
// Get a column's name.
public String getColumnName(int col) {
return columnNames[col];
}
// Get a column's class.
public Class getColumnClass(int col) {
return columnClasses[col];
}
// Get table's row count.
public int getRowCount() {
return DownloadManagerSystem.getArrayList().size();
}
// Get value for a specific row and column combination.
public Object getValueAt(int row, int col) {
Download download = (Download)DownloadManagerSystem.getArrayList().get(row);
switch (col) {
case 0: // URL
return download.getUrl();
case 1: // Name
return download.getName();
case 2: // Size
int size = download.getSize();
return (size == -1) ? "" : Integer.toString(size);
case 3: // Progress
return new Float(download.getProgress());
case 4://Status
return Download.STATUSES[download.getStatus()];
case 5: //TimeLeft
return download.getTimeLeft();
case 6://TransferRate
return download.getSpeed();
case 7:// Date
return download.getDate();
}
return "";
}
/* Update is called when a Download notifies its
observers of any changes */
public void update(Observable o, Object arg) {
int index = DownloadManagerSystem.getArrayList().indexOf(o);
// Fire table row update notification to table.
fireTableRowsUpdated(index, index);
}
}
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.