Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Java
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
Java
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
Java
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
SQL
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)
)
Posted

Read here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html[^]

Why does you ImageRenderer(*) get an Object that is casted to String (obviously a text?) It should be served an object of type ProductPicture.

* be careful with those names, some might be already taken and are able to cause confusion. Better to use a suffix like "MyImageRenderer" or "ProductImageRenderer".
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900