Uday A. Navapara
Uday A. Navapara

Reputation: 1265

how to repaint the data of the JTable

i have write following method to create the table but when ever it called every time the list of the task is different but the table cannot change the value of the table

 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);
}

please give me the solution of my problem as soon as possible fast

Upvotes: 0

Views: 147

Answers (3)

Toufic
Toufic

Reputation: 382

you should only remove tblTaskList = new JTable(); from displayAllTaskByProjectId function and code will work properly.Use only this line tblTaskList = new JTable() only when init components

Upvotes: 1

Aditya
Aditya

Reputation: 1344

The problem for data not being refreshed is that you are giving a call to fireTableDataChanged before setting the model for table,

Made changes in your code like this:

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);

}

Look into this, Swing Tutorial for JTables for more info.

Upvotes: 0

mKorbel
mKorbel

Reputation: 109813

please give me the solution of my problem as soon as possible fast

(as soon as possible fast and first)

Upvotes: 1

Related Questions