By dwayne

2011-02-09 04:42:03 8 Comments

Can someone point me in the right direction on how to add an image into Java Table cell.


@Shinwar ismail 2019-10-05 00:01:09

1- add label to jtable ( create class for this)

 class LabelRendar implements TableCellRenderer{

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return (Component)value;


2- code jButton to add image

DefaultTableModel m = (DefaultTableModel) jTable1.getModel();
      jTable1.getColumn("image").setCellRenderer(new LabelRendar());  // call class 
      JLabel lebl=new JLabel("hello");
      lebl.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/bslogo120.png"))); // NOI18N
          m.addRow(new Object[]{"", "","",lebl});

@camickr 2011-02-09 15:49:37

JTable already provides a default renderer for icons. You just need to tell the table what data is stored in a given column so it can choose the appropriate renderer. This is done by overriding the getColumnClass(...) method:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JPanel
    public TableIcon()
        Icon aboutIcon = new ImageIcon("about16.gif");
        Icon addIcon = new ImageIcon("add16.gif");
        Icon copyIcon = new ImageIcon("copy16.gif");

        String[] columnNames = {"Picture", "Description"};
        Object[][] data =
            {aboutIcon, "About"},
            {addIcon, "Add"},
            {copyIcon, "Copy"},

        DefaultTableModel model = new DefaultTableModel(data, columnNames)
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class
            public Class getColumnClass(int column)
                return getValueAt(0, column).getClass();
        JTable table = new JTable( model );

        JScrollPane scrollPane = new JScrollPane( table );
        add( scrollPane );

    private static void createAndShowGUI()
        JFrame frame = new JFrame("Table Icon");
        frame.add(new TableIcon());
        frame.setLocationByPlatform( true );
        frame.setVisible( true );

    public static void main(String[] args)
        EventQueue.invokeLater(new Runnable()
            public void run()


@Stefan Reich 2016-09-21 15:27:02

Yeah it works! Thanks. Question: Why the setPreferredScrollableViewportSize line? Seems to work fine without it.

@camickr 2016-09-21 15:54:36

@StefanReich, It has nothing to do with displaying the icon in the table. The size of the frame is different when you do the pack(). Depends on your requirement if you use the method or not.

@Stefan Reich 2016-09-21 18:15:48

@camickr Ah, it's about pack(), OK.

@ayush 2011-02-09 04:48:25

Either create the imageicon up front:

ImageIcon icon = new ImageIcon("image.gif");
table.setValueAt(icon, row, column);

Or you can try overriding the renderer for your icon field:

static class IconRenderer extends DefaultTableCellRenderer {
  public IconRenderer() { super(); }

  public void setValue(Object value) {
    if (value == null) {

@gumuruh 2012-04-10 09:27:06

another example would be taken from here:

@Buffalo 2014-02-19 11:30:09

setIcon? where's that defined?

@Benas 2014-07-02 12:58:42

@Buffalo setIcon((ImageIcon)value);

@Frakcool 2016-02-02 02:43:27

@gumuruh link is down

Related Questions

Sponsored Content

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4051787 View
  • 3497 Score
  • 66 Answer
  • Tags:   java random integer

31 Answered Questions

[SOLVED] How to create a generic array in Java?

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

42 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

22 Answered Questions

[SOLVED] How can I get the current stack trace in Java?

  • 2009-07-01 13:13:01
  • ripper234
  • 521021 View
  • 1001 Score
  • 22 Answer
  • Tags:   stack-trace java

10 Answered Questions

[SOLVED] A 'for' loop to iterate over an enum in Java

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

15 Answered Questions

[SOLVED] Why can't I use switch statement on a String?

35 Answered Questions

[SOLVED] How do I break out of nested loops in Java?

30 Answered Questions

[SOLVED] How to avoid Java code in JSP files?

  • 2010-07-05 07:24:06
  • chmoelders
  • 285320 View
  • 1673 Score
  • 30 Answer
  • Tags:   java jsp scriptlet

Sponsored Content