2013-03-25 69 views
0

我正在使用JTable顯示有關從JFileChooser中選擇的文件的信息。當我點擊上傳按鈕時,我的實際上傳將通過從表中選擇所選文件開始,它將嘗試更新JTable中相應文件的文件上傳狀態。這裏,當我試圖在文件上傳過程中更新JTable的狀態字段中的值時,它僅更新了幾次。它從0開始,直接更新爲100,但無法看到其他進度值。請看看到下面的代碼,JTable單元不會隨定期更新而更新

我的表編號:

uploadTableModel = new UploadTabModel(); 
uploadTable = new JTable(uploadTableModel); 
uploadTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN); 
uploadTable.setAutoCreateRowSorter(false); 
uploadTable.setShowGrid(false); 
uploadTable.setVisible(true); 
JScrollPane tablePane = new JScrollPane(); 
tablePane.setViewportView(uploadTable); 

我的表型號:

public class UploadTabModel extends AbstractTableModel { 

    private List<String> names = new ArrayList<String>(); 
    private List<FileDTO> data = new ArrayList<FileDTO>(); 
    public CMUploadTabModel() { 

     names.add("Name"); 
     names.add("Size"); 
     names.add("Status"); 
    } 
    private static final long serialVersionUID = 3151839788636790436L; 

    @Override 
    public int getColumnCount() { 
     return names.size(); 
    } 

    @Override 
    public int getRowCount() { 
     // TODO Auto-generated method stub 
     return data.size(); 
    } 

    @Override 
    public Object getValueAt(int row, int col) { 
     FileDTO file = data.get(row); 
     switch (col) { 
     case 0: 
      return file.getFileName(); 
     case 1: 
      return file.getSize(); 
     case 2: 
      return file.getStatus(); 
     } 
     return file.getFileName(); 
    } 

    @Override 
    public void setValueAt(Object arg0, int rowIndex, int columnIndex) { 
     FileDTO file = data.get(rowIndex); 
     switch (columnIndex) { 
      case 2: 
       file.setStatus((Integer) arg0); 
       break; 
     } 
    } 

    public void addRow(FileDTO file) { 
     this.data.add(file); 
     this.fireTableRowsInserted(data.size() - 1, data.size() - 1); 
    } 

    public String getColumnName(int columnIndex) { 
     return names.get(columnIndex); 
    } 


    @Override 
    public Class<?> getColumnClass(int index) { 
     return getValueAt(0, index).getClass(); 
    } 
    public void updateProgress(int index, final int percentage) { 

     FileDTO file = data.get(index); 
     file.setStatus(percentage); 
     data.set(0, file); 
     setValueAt(percentage, index, 2); 
     fireTableRowsUpdated(index, 2); 

    } 
} 

我的文件模型組件:

public class FileDTO { 

    private String fileName; 
    private Long size; 
    private Integer status =0; 

    public FileDTO(File file) { 

     this.fileName = file.getName(); 
     this.size = file.length(); 
    } 

//setters & getters 

處理器從上傳更新表:

handler = new IProgressHandler() { 

     @Override 
     public void update(int index, int percentage) { 
      uploadTableModel.updateProgress(index,percentage); 

     } 
    }; 

敬請建議我實現這一目標。

+0

可能無關:您的模型不通知setValueAt - 這是必須的 – kleopatra 2013-03-25 17:10:00

回答

1

聽起來好像您的IProgressHandler正在EDT上執行,因此在上傳完成之前,GUI無法重新繪製。

請閱讀Swing教程Concurrency in Swing中的部分。您應該使用SwingWorker來完成此任務。