2014-03-24 77 views
0

我有寫下面的方法來創建表,但是當以往任何時候都稱爲每次任務的列表是不同的,但該表不能更改表的值的數據如何重新繪製的JTable中

public void displayAllTaskByProjectId(Task[] task) { 

    System.out.println("Success fully call"); 
    /** 
    * The table to display tasks related to the selected project and timer 
    * for that 
    */ 
    tblTaskList = new JTable(); 
    LOG.info(" get here"); 
    int length; 

    DefaultTableModel dm = new DefaultTableModel(0, 0) { 
     private static final long serialVersionUID = 1L; 

     public Class getColumnClass(int column) { 
      return getValueAt(0, column).getClass(); 
     }; 

     public boolean isCellEditable(int row, int column) { 
      switch (column) { 
      case 5: 
       return true; 
      default: 
       return false; 
      } 

     }; 
    }; 
    dm.fireTableDataChanged(); 
    String header[] = new String[] { "Prority", "Task Title", "Start", 
      "Pause", "Stop", "Statulses" }; 
    dm.setColumnIdentifiers(header); 

    // Add image icon in table 
    ImageIcon imgStart = new ImageIcon("../../../../start.png"); 
    ImageIcon imgStop = new ImageIcon("../../../../stop.png"); 
    ImageIcon imgPause = new ImageIcon("../../../../pause.png"); 
    length = task.length; 
    for (int count = 0; count < length; count++) { 
     dm.addRow(new Object[] { count, task[count].getTaskTitle() + count, 
       imgStart, imgPause, imgStop, "Active" }); 
    } 
    tblTaskList.setModel(dm); 
    TableColumn tbcStatus = tblTaskList.getColumnModel().getColumn(5); 
    JComboBox cmbStatus = new JComboBox(); 
    cmbStatus.setModel(new DefaultComboBoxModel(new String[] { "Active", 
      "Close", "Deactive", "Offline" })); 
    tbcStatus.setCellEditor(new DefaultCellEditor(cmbStatus)); 

    tblTaskList.getColumnModel().getColumn(0).setMinWidth(0); 
    tblTaskList.getColumnModel().getColumn(0).setMaxWidth(0); 
    tblTaskList.getColumnModel().getColumn(1).setPreferredWidth(350); 
    tblTaskList.getColumnModel().getColumn(2).setPreferredWidth(35); 
    tblTaskList.getColumnModel().getColumn(3).setPreferredWidth(35); 
    tblTaskList.getColumnModel().getColumn(4).setPreferredWidth(25); 

    tblTaskList.getTableHeader().setReorderingAllowed(false); 
    tblTaskList.getTableHeader().setResizingAllowed(false); 
    tblTaskList.setBounds(93, 34, 614, 324); 

    jspTasklist = new JScrollPane(tblTaskList); 
    jspTasklist.setOpaque(false); 
    jspTasklist.setFocusTraversalPolicyProvider(true); 
    jspTasklist.setBounds(108, 34, 605, 335); 
    frmTaskList.getContentPane().add(jspTasklist); 
} 

請儘快快

+1

可能重複[添加到ArrayList後刷新JTable](http://stackoverflow.com/questions/22437998/ refresh-jtable-after-add-to-arraylist)(和其他許多人)。 –

回答

1

你應該只刪除tblTask​​List = new JTable();從displayAllTask​​ByProjectId函數和代碼將正常工作。只使用此行tblTask​​List = new JTable()僅當初始化組件

0

對於沒有被刷新數據的問題是,你正在設置表模型前給予fireTableDataChanged一個電話,

在你的代碼作出這樣的變化:

公共無效displayAllTask​​ByProjectId(任務[]任務){

System.out.println("Success fully call"); 
/** 
* The table to display tasks related to the selected project and timer 
* for that 
*/ 
tblTaskList = new JTable(); 
LOG.info(" get here"); 
int length; 

DefaultTableModel dm = new DefaultTableModel(0, 0) { 
    private static final long serialVersionUID = 1L; 

    public Class getColumnClass(int column) { 
     return getValueAt(0, column).getClass(); 
    }; 

    public boolean isCellEditable(int row, int column) { 
     switch (column) { 
     case 5: 
      return true; 
     default: 
      return false; 
     } 

    }; 
}; 
tblTaskList.setModel(dm); 

String header[] = new String[] { "Prority", "Task Title", "Start", 
     "Pause", "Stop", "Statulses" }; 
dm.setColumnIdentifiers(header); 

// Add image icon in table 
ImageIcon imgStart = new ImageIcon("../../../../start.png"); 
ImageIcon imgStop = new ImageIcon("../../../../stop.png"); 
ImageIcon imgPause = new ImageIcon("../../../../pause.png"); 
length = task.length; 
for (int count = 0; count < length; count++) { 
    dm.addRow(new Object[] { count, task[count].getTaskTitle() + count, 
      imgStart, imgPause, imgStop, "Active" }); 
} 
dm.fireTableDataChanged(); 
TableColumn tbcStatus = tblTaskList.getColumnModel().getColumn(5); 
JComboBox cmbStatus = new JComboBox(); 
cmbStatus.setModel(new DefaultComboBoxModel(new String[] { "Active", 
     "Close", "Deactive", "Offline" })); 
tbcStatus.setCellEditor(new DefaultCellEditor(cmbStatus)); 

tblTaskList.getColumnModel().getColumn(0).setMinWidth(0); 
tblTaskList.getColumnModel().getColumn(0).setMaxWidth(0); 
tblTaskList.getColumnModel().getColumn(1).setPreferredWidth(350); 
tblTaskList.getColumnModel().getColumn(2).setPreferredWidth(35); 
tblTaskList.getColumnModel().getColumn(3).setPreferredWidth(35); 
tblTaskList.getColumnModel().getColumn(4).setPreferredWidth(25); 

tblTaskList.getTableHeader().setReorderingAllowed(false); 
tblTaskList.getTableHeader().setResizingAllowed(false); 
tblTaskList.setBounds(93, 34, 614, 324); 

jspTasklist = new JScrollPane(tblTaskList); 
jspTasklist.setOpaque(false); 
jspTasklist.setFocusTraversalPolicyProvider(true); 
jspTasklist.setBounds(108, 34, 605, 335); 
frmTaskList.getContentPane().add(jspTasklist); 

}

調查此,Swing Tutorial for JTables欲瞭解更多信息。