⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadstablemodel.java

📁 1.代码全由java书写 2.支持http1.1的可续传下载 3.GUI采用的是Java Swing 4.设计模式为Observer 5.采用了多线程机制
💻 JAVA
字号:
//创建于2007-12-22 by刘浩
//最后更改于2008-12-29 by兰冲
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

// 这个类管理table中的数据
class DownloadsTableModel extends AbstractTableModel
  implements Observer
{
  // 表格的列名
  private static final String[] columnNames = {"URL", "大小",
    "完成百分比", "状态"};

  // 每个列对应的类型
  private static final Class[] columnClasses = {String.class,
    String.class, JProgressBar.class, JLabel.class};

  // 下载任务列表
  private ArrayList downloadList = new ArrayList();

  // 添加新的下载任务到列表中
  public void addDownload(Download download) {
    // Register to be notified when the download changes.
    download.addObserver(this);

    downloadList.add(download);

    // 触发添加行事件
    fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
  }

  // 返回行号对下的下载
  public Download getDownload(int row) {
    return (Download) downloadList.get(row);
  }

  //从download列表中删除指定download
  public void clearDownload(int row) {
    downloadList.remove(row);

    // 激发删除行事件
    fireTableRowsDeleted(row, row);
  }

  // 返回表格列数
  public int getColumnCount() {
    return columnNames.length;
  }

  // 返回指定列的名称
  public String getColumnName(int col) {
     return columnNames[col];
  }

  // 返回指定列的类型
  public Class getColumnClass(int col) {
    return columnClasses[col];
  }

  // 返回表格的行数
  public int getRowCount() {
    return downloadList.size();
  }

  // 返回位于指定行指定列的表格值
  public Object getValueAt(int row, int col) {
    Download 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
        return Download.STATUSES[download.getStatus()];
    }
    return "";
  }

  /* 当download告诉本类其发生了改变时调用本函数 */
  public void update(Observable o, Object arg) {
    int index = downloadList.indexOf(o);

    //激活更新行事件
    fireTableRowsUpdated(index, index);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -