如何重绘JTable的数据

时间:2014-03-24 11:25:59

标签: java swing user-interface model 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);
}

请尽快快速解决我的问题

3 个答案:

答案 0 :(得分:1)

  

请尽快快速解决我的问题

(尽快和第一次)

答案 1 :(得分:1)

你应该只删除tblTask​​List = new JTable();从displayAllTask​​ByProjectId函数和代码将正常工作。仅在init组件

时才使用此行tblTask​​List = new JTable()

答案 2 :(得分:0)

数据未刷新的问题是您在为表格设置模型之前打电话给fireTableDataChanged

对代码进行了更改,如下所示:

public void displayAllTask​​ByProjectId(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;
        }

    };
};
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以获取更多信息。