1

I have write the following code to put image in JTable but in a place of image it print path of the image on to the data

    DefaultTableModel dm = new DefaultTableModel(0, 0);
    String header[] = new String[] { "Status", "Task Title", "Start",
            "Pause", "Stop", "Statulses" };
    dm.setColumnIdentifiers(header);
    tblTaskList.setModel(dm);

    ImageIcon imgStart = new ImageIcon(getClass().getResource("start.gif"));
    ImageIcon imgStop = new ImageIcon("stop.png");
    ImageIcon imgPause = new ImageIcon("pause.gif");

    for (int count = 1; count <= 10; count++) {
        Vector<Object> data = new Vector<Object>();
        data.add(count);
        data.add("Project Title" + count);
        data.add(imgStart);
        data.add(imgPause);
        data.add(imgStop);
        data.add("Status");
        System.out.println("test :- " + count);
        dm.addRow(data);
        // tblTaskList.setModel(new DefaultTableModel(new Object[][] { {
        // count, "title1", "start", "stop", "pause", "status" }, },
        // new String[] { "status", "Task Title", "Start", "Stop",
        // "Pause", "Status" }));
    }
    tblTaskList.getColumnModel().getColumn(0).setPreferredWidth(31);
    tblTaskList.getColumnModel().getColumn(1).setPreferredWidth(346);
    tblTaskList.getColumnModel().getColumn(2).setPreferredWidth(33);
    tblTaskList.getColumnModel().getColumn(3).setPreferredWidth(31);
    tblTaskList.getColumnModel().getColumn(4).setPreferredWidth(28);

    tblTaskList.setBounds(93, 34, 614, 160);
    frmTaskList.getContentPane().add(tblTaskList);

I have try that 3 different ways but cannot find the proper output. suggest me the way to do this properly

Cœur
  • 37,241
  • 25
  • 195
  • 267
Uday A. Navapara
  • 1,237
  • 5
  • 20
  • 40
  • Have a look in here: http://stackoverflow.com/questions/4941372/how-to-insert-image-into-jtable-cell/4947154#4947154 – Anto Mar 13 '14 at 10:04

2 Answers2

4
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You have miss one more step and it is to override the getColumnClass() in TableModel

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

this will replace the image path with it's orignal class image