?
Solved

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

Posted on 2006-07-16
9
Medium Priority
?
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 total points
ID: 17120249
cellimg =new ImageIcon(new File(s).toUrl(),"Cell Image");
0
 
LVL 35

Accepted Solution

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

cellimg =new ImageIcon(new File(s).toURI().toUrl(),"Cell Image");
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 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