Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 33: Creating a Download Manager in Java 979


This method first registers itself with the newDownloadas anObserverinterested in
receiving change notifications. Next, theDownloadis added to the internal list of downloads
being managed. Finally, a table row insertion event notification is fired to alert the table that
a new row has been added.


The clearDownload( ) Method

TheclearDownload( )method, shown next, removes aDownloadfrom the list of managed
downloads:


// Remove a download from the list.
public void clearDownload(int row) {
downloadList.remove(row);


// Fire table row deletion notification to table.
fireTableRowsDeleted(row, row);
}


After removing theDownloadfrom the internal list, a table row deleted event notification
is fired to alert the table that a row has been deleted.


The getColumnClass( ) Method

ThegetColumnClass( )method, shown here, returns the class type for the data displayed
in the specified column:


// Get a column's class.
public Class getColumnClass(int col) {
return columnClasses[col];
}


All columns are displayed as text (that is,Stringobjects) except for the Progress column,
which is displayed as a progress bar (which is an object of typeJProgressBar).


The getValueAt( ) Method

ThegetValueAt( )method, shown next, is called to get the current value that should be
displayed for each of the table’s cells:


// Get value for a specific row and column combination.
public Object getValueAt(int row, int col) {
Download download = downloadList.get(row);
switch (col) {
case 0: // URL
return download.getUrl();
case 1: // Size
int size = download.getSize();
return (size == -1)? "" : Integer.toString(size);
case 2: // Progress
return new Float(download.getProgress());
case 3: // Status

Free download pdf