在JTable中为每一行显示不同的图像 [英] Show different image for each row in JTable

查看:77
本文介绍了在JTable中为每一行显示不同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序来管理我项目的商店产品。我遇到了问题,我真的需要你的想法来解决它。



我使用DefaultTableCellRenderer在主屏幕的产品基本信息表中成功显示图像。但我只能为所有产品显示1张图片。每个产品都有不同的图像,所以我需要在产品基本信息JTable中为每一行显示不同的图像。



这是我工作的一些部分。 />


这是我的DefaultTableCellRenderer扩展类:

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





这是我的SQL创建表脚本。我将产品图像的路径存储在数据库中



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)
)

推荐答案

在此处阅读: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html [ ^ ]



为什么ImageRenderer(*)得到一个被转换为String的对象(显然是一个文本?)它应该是一个ProductPicture类型的对象。



*小心这些名字,有些可能已经被采取并且能够引起混淆。最好使用像MyImageRenderer或ProductImageRenderer这样的后缀。
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".


这对你有帮助...





http://stackoverflow.com/questions/7049494/how- to-get-icon-from-jtable [ ^ ]
this will help you...


http://stackoverflow.com/questions/7049494/how-to-get-icon-from-jtable[^]


这篇关于在JTable中为每一行显示不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆