0

プロジェクトの店舗の商品を管理するアプリケーションを構築しています。私は問題に直面しており、それを解決するにはあなたのアイデアが本当に必要です.

DefaultTableCellRenderer を使用して、メイン画面の製品基本情報テーブルに画像を表示することに成功しました。ただし、すべての製品に対して 1 つの画像しか表示できません。商品ごとに画像が異なるため、商品基本情報 JTable の行ごとに異なる画像を表示する必要があります。

これが私の作品の一部です。

これは私の DefaultTableCellRenderer 拡張クラスです:

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

カスタム製品基本情報テーブル モデル

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

...そして show result メソッドで

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

これは私の 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)
)

どうもありがとうございました。

4

1 に答える 1

1

カスタム レンダラーを作成する必要はありません。JTable は、アイコンを表示するためのレンダラーを既にサポートしています。したがって、必要なことは次のとおりです。

  1. 行ごとに他のテキストを保存するのと同じ方法で、各行のモデルにアイコンを保存します。
  2. モデルの getColumnClass() メソッドをオーバーライドして、アイコン レンダラーを使用するようテーブルに指示します。何かのようなもの:

    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;
    }
    
于 2013-09-11T03:37:27.780 に答える