2013-03-25 74 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来完成此任务。