Link to home
Start Free TrialLog in
Avatar of tyweed420
tyweed420

asked on

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;
        }
Avatar of girionis
girionis
Flag of Greece image

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
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry typo:

cellimg =new ImageIcon(new File(s).toURI().toURL(),"Cell Image");
Avatar of tyweed420
tyweed420

ASKER

ok so it seemed to work sort of.... the problem is it displays "Cell Image" instead of the actual image ? any ideas why???
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
How do you add the image in the table cell?
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?
Because you add a table to a table model.