0

I know this will be a duplicate question,but I can't find the answer on my case. I have successfully created my JTable with data from database. In my JTable, one of the column holds images. I have tried to show these images with getColumnClass(int column), but I don't understand how to use this method & no good tutorial I found that I can understand ... How can I show these images in my JTable?

import java.sql.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class BurgerData extends JFrame
{
JTable BurgerList;

public BurgerData()
{
    setSize(800,800);
    setLayout(new FlowLayout());
    setVisible(true);       
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/image","root","");
        Statement stmnt = con.createStatement();
        ResultSet rs = stmnt.executeQuery("SELECT * FROM `icon`");

        ResultSetMetaData rsmetadata = rs.getMetaData();
        int col = rsmetadata.getColumnCount();
        DefaultTableModel dtm = new DefaultTableModel();

        Vector<String> col_name = new Vector<String>();
        Vector<Object> row_data = new Vector<Object>();

        for(int i=1;i<=col;i++)
        {
            col_name.addElement(rsmetadata.getColumnName(i));
        }

        dtm.setColumnIdentifiers(col_name);

        while(rs.next())
        {
            row_data = new Vector<Object>();

            for(int i=1;i<=col;i++)
            {
                row_data.addElement(rs.getObject(i));
            }

            dtm.addRow(row_data);
        }

    BurgerList = new JTable( dtm )
    {
        public Class getColumnClass(int column)
        {
            return getValueAt(0, column).getClass();
        }
    };

    BurgerList.setModel(dtm);

    add(BurgerList);

    }

    catch(SQLException e)
    {
        System.out.println("Unknown Error");
    }

    catch(Exception eg)
    {
        System.out.println("Unknown Error");
    }
}

public static void main(String args[])
{
    BurgerData n = new BurgerData();
}
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Hasan
  • 49
  • 1
  • 7
  • 1
    Divide and conquer, create a simple class with a `JTable` with 1 or 2 columns, where you hardcode an image (as shown [here](http://stackoverflow.com/questions/4941372/how-to-insert-image-into-jtable-cell)), then if that goes right, try to pull the images from the DB if that doesn't work, then you know that what's wrong is the DB thing, for better help sooner post a valid [mcve] that we can copy-paste w/o DB connections because this question isn't related to it. Also you're making your frame visible before you have added all your elements to it – Frakcool Dec 21 '16 at 18:20
  • I have already inserted Images (From database) in my JTable "BurgerList" as Object.But when i'm adding JTable to frame,everything is ok but the Images are not showing (It is showing some string like : [B@6b4455f0] ) – Hasan Dec 21 '16 at 18:41
  • I don't see any `ImageIcon` instance in your code, your data should be of type `ImageIcon` not `Object` then – Frakcool Dec 21 '16 at 18:46

1 Answers1

4

I have already inserted Images

It is showing some string like : [B@6b4455f0] )

A JTable does not have a default renderer for an Image, so you are seeing the toString() representation of the Image.

Instead you need to create an ImageIcon using the Image. The JTable will then use a JLabel to renderer the Icon.

For example: How to set icon in a column of JTable?

row_data.addElement(rs.getObject(i));

So you can't just copy all the Objects to the table model. You need to check if the Object is an Image and then create the ImageIcon and add it to the model.

The other solution is to create a custom renderer for the Image class (then you can just copy the Objects directly to the model). See the section from the Swing tutorial on Using Custom Renderers for more information.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Oh,this is helpful . – Hasan Dec 21 '16 at 19:52
  • I know which specific column got the Image,so,if i do it with vector - what will be my vector type ? – Hasan Dec 21 '16 at 20:24
  • @Hasan, Did you look at the code in the link I provided? What data type is used in that link? – camickr Dec 21 '16 at 21:24
  • Yes,i have also seen your link before while i was searching for answers..I think i have to use ImageIcon type for that particular column – Hasan Dec 22 '16 at 05:08
  • @Hasan, Your previous comment asked about the Vector, not the column. It is two different things. The Vector hold Objects because you can have String, Integer, ImageIcon for each row, the getColumnClass return the appropriate value for the column of each row. – camickr Dec 22 '16 at 15:49
  • @Hasan, glad it helped. Don't forget to "accept" the answer by clicking on the checkmark so people know the problem has been solved. – camickr Dec 24 '16 at 16:18