JTable with icons

hi I need to insert an icon in each row of the JTable, is there any solution ,my table extends DefaultTable modelregvinoMessage was edited by: chand_vino
[195 byte] By [chand_vinoa] at [2007-10-3 2:42:44]
# 1
Add one more column to the model and use a custom renderer for it to display an icon.
PhHeina at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 2

> is there any solution

Yes, learn to search the forum before posting questions. This question has been asked and answer before. Keywords like "jtable icon" aren't too difficult to think of now are they?

There is no need to create a custom renderer. JTable has a default renderer for ImageIcon, you just need to tell the table what type of data is being store in each column.

camickra at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 3

hi

I can insert the icon in the table when i enter the static rows , but i need to insert the icons when i insert the data into the table on run time the icon will insert along with the row which is to be appeared left align icon in the first cell

any solutions ,

reg

vino

chand_vinoa at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 4

The rendering is the same for Icons added statically or dynamically. I can't guess what the problem might be.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 5

> The rendering is the same for Icons added statically

> or dynamically. I can't guess what the problem might

> be.

my problem i populate the jtable from the Ini file

String cname[]={aa,yy,zz};

Object[][] dataTable =readIni();

// adding items to table

model = new DefaultTableModel(dataTable, columnNames);

jTable1 = new JTable(model)

in the ReadIni() i read data from the ini file along with this data how can i insert icons plz tell me the solution its very urgent to me, i struggle with this for the past three days

advanced thanks

reg

vino

chand_vinoa at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 6
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=604783
camickra at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 7
> http://forum.java.sun.com/thread.jspa?forumID=57&threa> dID=604783 it is working for onlyjpg , for gif it just displays the path of the image file regvino
chand_vinoa at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 8

> it is working for only jpg , for gif it just displays the path of the image file

Did you read my first comment:

you just need to tell the table what type of data is being store in each column.

If the data is displayed as a pathname, then the program thinks you have a string in that cell.

Did you look at my example? I gave you working code. I can't debug invisible code. I'm not a mind reader I don't know what your code looks like.

camickra at 2007-7-14 20:31:00 > top of Java-index,Desktop,Core GUI APIs...
# 9

> > it is working for only jpg , for gif it just

> displays the path of the image file

>

> Did you read my first comment:

>

> you just need to tell the table what type of data

> is being store in each column.

>

> If the data is displayed as a pathname, then the

> program thinks you have a string in that cell.

>

> Did you look at my example? I gave you working code.

> I can't debug invisible code. I'm not a mind reader I

> don't know what your code looks like.

import java.awt.*;

import javax.swing.*;

import javax.swing.table.*;

public class TableIcon extends JFrame

{

public TableIcon()

{

String[] columnNames = {"Picture", "Description"};

Object[][] data =

{

{new ImageIcon("copy16.gif"), "Copy"},

{new ImageIcon("add16.gif"), "Add"},

};

DefaultTableModel model = new DefaultTableModel(data, columnNames);

JTable table = new JTable( model )

{

// Returning the Class of each column will allow different

// renderers to be used based on Class

public Class getColumnClass(int column)

{

return getValueAt(0, column).getClass();

}

};

table.setPreferredScrollableViewportSize(table.getPreferredSize());

JScrollPane scrollPane = new JScrollPane( table );

getContentPane().add( scrollPane );

}

public static void main(String[] args)

{

TableIcon frame = new TableIcon();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

}

This is the code given in the link which u provide in the previous response

reg

vino

chand_vinoa at 2007-7-14 20:31:01 > top of Java-index,Desktop,Core GUI APIs...
# 10
> This is the code given in the link which u provide in the previous responseI know what the code looks like. So what is your problem? The first column contains a ImageIcon the second column contains a String. How do you expect the table the render the second column as an Image?
camickra at 2007-7-14 20:31:01 > top of Java-index,Desktop,Core GUI APIs...