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.

3 comments

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

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

 class LabelRendar implements TableCellRenderer{

    @Override
    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 );
        table.setPreferredScrollableViewportSize(table.getPreferredSize());

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

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

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

}

@Sierox 2015-08-28 15:01:19

I think this answer is better...

@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) {
      setText("");
    }
    else
    {
      setIcon(value);
    }
}

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

another example would be taken from here: javareference.com/jrexamples/viewexample.jsp?id=125

@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

30 Answered Questions

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

41 Answered Questions

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

43 Answered Questions

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

16 Answered Questions

[SOLVED] What is a JavaBean exactly?

65 Answered Questions

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

  • 2008-12-12 18:20:57
  • user42155
  • 3905427 View
  • 3361 Score
  • 65 Answer
  • Tags:   java random integer

10 Answered Questions

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

  • 2009-07-09 16:24:03
  • Nick Meyer
  • 451474 View
  • 853 Score
  • 10 Answer
  • Tags:   java enums for-loop

35 Answered Questions

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

  • 2009-05-20 09:07:43
  • boutta
  • 1087905 View
  • 1745 Score
  • 35 Answer
  • Tags:   java loops

57 Answered Questions

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

30 Answered Questions

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

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

14 Answered Questions

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

Sponsored Content