This discussion is archived
4 Replies Latest reply: Feb 15, 2013 6:50 AM by DarrylBurke RSS

showing image in jtable..problem of update?

818772 Newbie
Currently Being Moderated
Dear all,
I need your help.
I have a list of object, object properties are a name (string) and a path (string).
In my swing jtable I need to show string and an image, that is read from path.
So I iterate list and for each iteration I do
                //get name
                String csName = ...
                //get array of byte from path
                byte[] cImg = csFullFile.getBytes();
                
 
                Icon icon = new ImageIcon(cImg);
                
                Object rowData[] = new Object[]{name, icon};
                //add row
                dtm.addRow(rowData);
What happens it that list is populated with correct number of rows...property name is shown but it's not shown image.
I think there is some problem with update of cell or layout...could you help me?
What code I've to run to show image in the jtable?

Thanks,
Regards
  • 1. Re: showing image in jtable..problem of update?
    camickr Expert
    Currently Being Moderated
    You need to tell the table what type of data is stored in each column so the table can use the appropriate renderer. Here is a simple example:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableIcon extends JFrame
    {
         public TableIcon()
         {
              ImageIcon aboutIcon = new ImageIcon("about16.gif");
              ImageIcon addIcon = new ImageIcon("add16.gif");
              ImageIcon copyIcon = new ImageIcon("copy16.gif");
    
              String[] columnNames = {"Picture", "Description"};
              Object[][] data =
              {
                   {aboutIcon, "About"},
                   {addIcon, "Add"},
                   {copyIcon, "Copy"},
              };
    
              DefaultTableModel model = new DefaultTableModel(data, columnNames);
              JTable table = new JTable( model )
              {
                   //  Returning the Class of each column will allow different
                   //  renderers to be used based on Class
                   public Class getColumnClass(int column)
                   {
                        return getValueAt(0, column).getClass();
    //                    return (column == 0) ? Icon.class : Object.class;
                   }
              };
              table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
              JScrollPane scrollPane = new JScrollPane( table );
              getContentPane().add( scrollPane );
         }
    
         public static void main(String[] args)
         {
              TableIcon frame = new TableIcon();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setVisible(true);
         }
    
    }
  • 2. Re: showing image in jtable..problem of update?
    818772 Newbie
    Currently Being Moderated
    Thanks for your reply...
    the question is that I already did it.
    In fact I defined my tablemodel and overriden method
    @Override
        public Class getColumnClass(int col) {
            if (allegati)
            {
                if (col == 1) {//second column accepts only Integer values
                    System.out.println("override in corso");
                    return Icon.class;
                }
                if (col == 0) {
                    
                    return String.class;  //other columns accept String values
                }
            }
    }
    Additionally, that worked for me until some days ago, before my change of logic. Before I take blob from db and shown in the jtable correcty..now I read it from file, I obtain dynamically blob and I put in the same jtable...it's just changed source, that before was blob in the db now is a file, from which I obtain blob...that's why I think about some update of gui issue...no help for me?
    Thanks
  • 3. Re: showing image in jtable..problem of update?
    990001 Newbie
    Currently Being Moderated
    I have implied that code and they don't give any exception but problem is that at that place of Image or Icon that shows path of image.

    what wiil i do?
  • 4. Re: showing image in jtable..problem of update?
    DarrylBurke Guru Moderator
    Currently Being Moderated
    986998 wrote:
    I have implied that code and they don't give any exception but problem is that at that place of Image or Icon that shows path of image.

    what wiil i do?
    Get your own thread, for starters.

    db

    THREAD CLOSED

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points