Display an image in a jtable column tried everything not sure how to make this work!

I have a jtable with the following columns

String[] columnNames = {"Cover","Title","Genre","format","Play Movie"};

the first column are going to be images, covers of movies to be exact. How i fill my columns is that I have a textfile with all the information seperated by "|" I parse this info with a loop and each column is added to the vector. Once each string is added to the vector i add that vector to an arraylist and move to the next line. So, at the end of the loop I have an arraylist full of vectors each is a row of my table. Then i simply just add each to the table and my table shows up.

ok, now for the problem...... my first string at each line of the file is a url to the cover image i'm looking to display. I'm able to detect this string at each line because it is the first string parsed. At this detection i'm trying to convert it into a ImageIcon and then simply add it to the vector. However, when everything displays that row no longer displays and everything is shifgted left one.

here is text file it is parsing
============================

C:\Documents and Settings\Owner\Desktop\java workspace\Movie Front\superman.jpg| Superman | Action | DIVX|  "C:\Documents and Settings\Owner.YOUR-49431BA77A.000\My Documents\My Videos\paola.mpeg" /fullscreen
C:\Documents and Settings\Owner\Desktop\java workspace\Movie Front\batman.jpg| Batman | Action | AVI| "C:\Documents and Settings\Owner.YOUR-49431BA77A.000\My Documents\My Videos\test.mpeg" /fullscreen

============================

So, how do i take that first string which points to my image and add that to the vector so when i add that vector row that image will display???


===========code======================
============So this reads one line then breaks it up into strings and adds each string to vector.Then that vector is added to an arraylist .then you have column one so you move down a line repeat!
 /**
    Read file and place contents into string array
    */
    public ArrayList readFile()
    {
        try
        {
            File file1 = new File("database.txt");
            if(!file1.exists())
            {
                FileWriter filewriter = new FileWriter("database.txt");
                filewriter.close();
            }

            list = new ArrayList();
            BufferedReader bufferedreader = new BufferedReader(new FileReader("database.txt"));
            String s2 = bufferedreader.readLine();
            int i = 0;
            for(; s2 != null; s2 = bufferedreader.readLine())
            {
                 Vector vector = new Vector(10);
                         for(StringTokenizer stringtokenizer = new StringTokenizer(s2, "|"); stringtokenizer.hasMoreTokens(); )
                         {
                                  if(i==0)  <==================== This is my url to image so try to create image then add to vector
                                     {

                                      try{
                                                String s = stringtokenizer.nextToken();
                                                System.out.println(s);
                                               cellimg =new ImageIcon(new URL(s),"Cell Image");
                                               vector.add(cellimg);

                               }
                               catch(Exception e){}
                                           //add image
                                }
                                  else <===============  otherwise just add string to vector and you get a column.
                                  vector.add(stringtokenizer.nextToken());
                                   i++;

                         }


                           list.add(vector);


                       i=0;
            }

            bufferedreader.close();

            return list;
        }
tyweed420Asked:
Who is Participating?
 
girionisConnect With a Mentor Commented:
better:

cellimg =new ImageIcon(new File(s).toURI().toUrl(),"Cell Image");
0
 
girionisCommented:
Hi tyweed420

I think you should create the URL differently since you need to indicate that this is a file URL and not an HTTP one.

Change this

>  cellimg =new ImageIcon(new URL(s),"Cell Image");

to

> cellimg =new ImageIcon(new URL("file://" + s),"Cell Image");

and see if it helps.

Cheers
0
 
objectsConnect With a Mentor Commented:
cellimg =new ImageIcon(new File(s).toUrl(),"Cell Image");
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
girionisCommented:
Sorry typo:

cellimg =new ImageIcon(new File(s).toURI().toURL(),"Cell Image");
0
 
tyweed420Author Commented:
ok so it seemed to work sort of.... the problem is it displays "Cell Image" instead of the actual image ? any ideas why???
0
 
tyweed420Author Commented:
ok i tried to remove the string parameter and this was what was displayed

file:/C:/Documents%20and%20Settings/Owner.YOUR-49431BA77A.000/Desktop/java%20workspace/Movie%20Front/superman.jpg

no picture in the cell yet!  but looks like i'm getting close
0
 
girionisCommented:
How do you add the image in the table cell?
0
 
tyweed420Author Commented:
fixed it all i had to do was add this to my table model

      public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}


why did this fix it?
0
 
girionisCommented:
Because you add a table to a table model.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.