Solved

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

Posted on 2006-07-16
9
258 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
 
LVL 35

Expert Comment

by:girionis
ID: 17120271
Sorry typo:

cellimg =new ImageIcon(new File(s).toURI().toURL(),"Cell Image");
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

707 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now