【问题标题】:How can I display an Image at the JTable如何在 JTable 中显示图像
【发布时间】:2015-06-12 12:34:28
【问题描述】:

我有一个带有自定义 DefaultTableModel 的 JTable。 我想在最后一列中插入一个 ImageIcon。所以我有这个代码:

public class MyTableModelMovimentiContiBancari extends defaultTableModel {

static String[] ColName = { "Cod.","Data","Descrizione", 
    "Codice Spesa","Uscite","Entrate",""};
public LinkedHashMap<Integer,ContoBancarioXOperazione> mappa;

public MyTableModelMovimentiContiBancari() {
    super(ColName, 0);
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
}
public Class<Float> getColumnClass(Float columnIndex) {
    return Float.class;    
}

@SuppressWarnings("unchecked")
public void stampaTabella(List<ContoBancarioXOperazione> mappa,ContoBancario c){
    ImageIcon aboutIcon = new ImageIcon(getClass().getResource("/resources/versamento.png"));
    Double saldoIniziale = c.getSaldoIniziale();
    this.addRow(new Vector());
    super.setValueAt(c.getDataRegistrazioneSaldoFormattata(), this.getRowCount()-1, 1);
    super.setValueAt("SALDO INIZIALE", this.getRowCount()-1, 2);
    super.setValueAt(decimalFormatter.format(saldoIniziale), this.getRowCount()-1, 5);
    for (ContoBancarioXOperazione conto : mappa) {
        int nColumn=0;
        this.addRow(new Vector());
        super.setValueAt(conto.getId().toString(), this.getRowCount()-1, nColumn++);
        super.setValueAt(conto.getDataRegistrazioneFormattata(), this.getRowCount()-1, nColumn++);



        super.setValueAt(conto.getIdSpesa()!=null && conto.getIdSpesa()>0 ? conto.getIdSpesa().toString() :"", this.getRowCount()-1, nColumn++);
        //se c'è stata una spesa l'importo va nell uscita
        if(conto.getIdSpesa()!=null && conto.getIdSpesa()>0){
            super.setValueAt(decimalFormatter.format(conto.getImporto()), this.getRowCount()-1, nColumn++);
            saldoIniziale -=conto.getImporto();
        }

        if(conto.getContoBanarioPrelievo()!=null &&
                conto.getContoBanarioPrelievo().getId()==c.getId()){
            //IN QUESTO CASO L IMPORTO VA NELLE USCITE
            super.setValueAt(decimalFormatter.format(conto.getImporto()), this.getRowCount()-1, 4);
            saldoIniziale -=conto.getImporto();
        }

        //se il movimento non ha spese, non ha conti di prelievo
        //allora significa che è una chiusura di giornata
        if(conto.getIdSpesa()==0 && conto.getContoBanarioPrelievo() == null){
            //IN QUESTO CASO L IMPORTO VA NELLE ENTRATE
            super.setValueAt(decimalFormatter.format(conto.getImporto()), this.getRowCount()-1, 5);
            saldoIniziale +=conto.getImporto();
        }
        super.setValueAt(aboutIcon, this.getRowCount()-1, 6);
    }
}   


}

这是包含我的 TableMolde 的 CustomTable

package com.mcsolution.table.MioJTable;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import com.mcsolution.table.MioTableModel.MyTableModelMovimentiContiBancari;

public class CustomTableMovimentiConti extends JTable {

    /**
     * 
     */
    private static final long serialVersionUID = 1180088009825388637L;
    private Font myFontTotale= new Font("Century Gothic", Font.BOLD, 17);
    @SuppressWarnings("unused")
    private MyTableModelMovimentiContiBancari modello;   // Sarà un riferimento al TableModel
    public CustomTableMovimentiConti(MyTableModelMovimentiContiBancari tableModel) {
        super(tableModel);
        modello = tableModel;
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {

        Component rendererComponent = super.prepareRenderer(renderer, row, column);

        if(row==0){
            rendererComponent.setBackground(new Color(200, 200, 200));
            rendererComponent.setFont(myFontTotale);
        }else if(row == super.getRowCount()-1){
            if(super.getValueAt(row, 2)!=null && 
                    super.getValueAt(row, 2).toString().equalsIgnoreCase("SALDO FINALE")){
                //ultima riga
                rendererComponent.setBackground(new Color(200, 200, 200));
                rendererComponent.setFont(myFontTotale);
            }           
        }else{
            rendererComponent.setBackground(Color.white);
        }
        return rendererComponent;
    }
}

使用此代码,我没有任何错误,但我看到的是完整路径,而不是在最后一列显示图像。

【问题讨论】:

  • “最后”列应返回Icon.class,最后一列应具有Icon 作为数据值...

标签: java swing jtable tablemodel


【解决方案1】:

How to use tables, Concepts: Editors and Renderers 中所述,默认渲染器能够处理IconImageIcon 对象。

您的TableModel 中的getColumnClass 方法需要为相应的列返回Icon.classImageIcon.class,并且TableModel 需要使用该列的正确数据来支持它

【讨论】:

  • 我已经更改了 MyTableModel 类中的 getColumnClass 方法。所以我现在有了这个: public Class getColumnClass(ImageIcon columnIndex) { return ImageIcon.class; // Le due Colonne sono numeri interi } 但不起作用
  • 您需要根据每列返回不同的类型,因为并非所有列都是图标...
猜你喜欢
  • 2017-04-06
  • 2020-06-08
  • 2017-05-07
  • 1970-01-01
  • 2015-10-16
  • 1970-01-01
  • 2018-09-27
相关资源
最近更新 更多