Question

I'm building an application to manage a store's product for my project. I'm facing a problem and I really need your idea to solve it.

I successfully show image in product basic info table at the main screen using DefaultTableCellRenderer. But I can only show 1 image for all product. Each product has a different image, so I need to display different image for each row in the product basic info JTable.

Here is some pieces of my work.

This is my DefaultTableCellRenderer extended class:

class ImageRenderer extends DefaultTableCellRenderer {
 JLabel lbl = new JLabel();

 ImageIcon icon = new ImageIcon("./src/comicbookandgamingzone/productpicture/NFS-Shift-2-Unleashed-Limited-Edition-Revealed-2.jpg");
 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
  boolean hasFocus, int row, int column) {
lbl.setText((String) value);
lbl.setIcon(icon);
lbl.setBounds(0, 0, 100, 100);
return lbl;
 }
}

The custom product basic info table model

class ProductTableModel extends AbstractTableModel{
String[] colname = {"ID","Picture","Name","Cost","In stock"};
ArrayList<Product> list;
public ProductTableModel(ArrayList<Product> prolist){
    this.list=prolist;
}
public String getColumnName(int col){
    return colname[col];
}
@Override
public int getRowCount() {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    return list.size();
}

@Override
public int getColumnCount() {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    return colname.length;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    switch(columnIndex){
        case 0: return list.get(rowIndex).ID;
        case 1: return list.get(rowIndex).Picture;
        case 2: return list.get(rowIndex).Name;
        case 3: return list.get(rowIndex).Cost;
        case 4: return list.get(rowIndex).Stock;
        default : return null;
    }

...and in the show result method

public void ShowResult(ArrayList<Product> list){
    tabProduct.setModel(new ProductTableModel(list));
    tabProduct.getColumnModel().getColumn(1).setCellRenderer(new ImageRenderer());
    tabProduct.setRowHeight(100);
}

This is my SQL create table script. I store the path of the product image in database

create table ProductDetails
(
ProductID int identity (1,1) not null,
ProductTypeID int foreign key references ProductType(TypeID),
ProductName text,
ProductPicture text,
ProductCost float,
ProductPoint int,
ProductStock int,
primary key (ProductID)
)

Thank you very much.

Was it helpful?

Solution

There is no need to create a custom renderer. JTable already supports a renderer to display an Icon. So all you need to do is:

  1. store the Icon in the model for each row, the same way that you store the other text for each row.
  2. Override the getColumnClass() method of the model to tell the table to use the Icon renderer. Something like:

    public Class getColumnClass(int column)
    {
        for (int row = 0; row < getRowCount(); row++)
        {
            Object o = getValueAt(row, column);
    
            if (o != null)
                return o.getClass();
        }
    
        return Object.class;
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top