How can I display an Image at the JTable

60 views Asked by At

I have a JTable, with custom DefaultTableModel. I want to insert a ImageIcon into the end column. So I have this code:

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


}

This is the CustomTable that contains my TableMolde

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

With this code, I don't have any error but instead of the show the image at the end column I see the full path.

1

There are 1 answers

2
MadProgrammer On

As explained in How to use tables, Concepts: Editors and Renderers, the default renderers are capable of handling Icon or ImageIcon objects.

You getColumnClass method from your TableModel needs to return either Icon.class or ImageIcon.class for the appropriate column and the TableModel needs to back it with the correct data for that column