Solved

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

Posted on 2006-07-16
9
283 Views
Last Modified: 2008-02-01
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;
        }
0
Comment
Question by:tyweed420
  • 5
  • 3
9 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17120105
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
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 17120249
cellimg =new ImageIcon(new File(s).toUrl(),"Cell Image");
0
 
LVL 35

Accepted Solution

by:
girionis earned 150 total points
ID: 17120267
better:

cellimg =new ImageIcon(new File(s).toURI().toUrl(),"Cell Image");
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:girionis
ID: 17120271
Sorry typo:

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

Author Comment

by:tyweed420
ID: 17123687
ok so it seemed to work sort of.... the problem is it displays "Cell Image" instead of the actual image ? any ideas why???
0
 

Author Comment

by:tyweed420
ID: 17124030
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
 
LVL 35

Expert Comment

by:girionis
ID: 17124335
How do you add the image in the table cell?
0
 

Author Comment

by:tyweed420
ID: 17125582
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
 
LVL 35

Expert Comment

by:girionis
ID: 17128164
Because you add a table to a table model.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question