Hasan
Hasan

Reputation: 49

Displaying Images in JTable

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();
}
}

Upvotes: 0

Views: 2954

Answers (1)

camickr
camickr

Reputation: 324118

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.

Upvotes: 4

Related Questions