如何重新绘制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);
}

请尽快给我解决问题的办法

你应该只删除tblTaskList = new JTable();从displayAllTaskByProjectId函数和代码将正常工作。只在init组件

时使用这一行tblTaskList = new JTable()

请尽快给我解决问题的办法

(as soon as possible fast and first)

  • 创建JTableDefaultTableModel为本地变量

  • 仅在模型中添加,删除,修改数据,不要重新创建整个结构,在紧急情况下在一个JTable的模型之间切换(已经在Swing GUI中可见)

  • 无论Task[] task是什么都必须通知EDT,然后代码model.add/remove/insertRow必须通过封装到invokeLater

  • 通知EDT

数据未被刷新的问题是,在为表设置模型之前,您正在调用fireTableDataChanged

像这样修改你的代码:

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

相关内容

  • 没有找到相关文章

最新更新