[Last Call] Learn how to a build a cloud-first strategyRegister Now


Swing: JTable with JLabel and images

Posted on 2011-10-13
Medium Priority
Last Modified: 2012-05-12
Please let me know how I can create a JTable with JLabels and images.

I want a 4X3 table with jlabels in the first 2 columns and images in the third column.

If possible, please provide some sample code.

Question by:dshrenik
  • 3
LVL 47

Expert Comment

ID: 36963957

You need to use TableCellRendereere
This is an example:

Author Comment

ID: 36963972
I am not sure how I can use JLabels and images inside a table. If possible, can you provide some sample code? Thanks!
LVL 47

Expert Comment

ID: 36963978
Table cell renderer retrns Component in this method:

 public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {

you should define this method so that you'll check row and column and either
return smple Lavble for some columns or return say JPanel
showing the image depeneding on the value of parameter column
LVL 47

Accepted Solution

for_yan earned 2000 total points
ID: 36964156
This is an example which takes image in the first ciolumn - find some good gif
file for it - mine is not too beautiful , but it works:

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new BorderLayout());

    MyTableModel model = new MyTableModel();

    JTable table = new JTable(model);
    table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(BorderLayout.CENTER, pane);
    frame.setSize(500, 400);

class MyTableModel extends AbstractTableModel {
  public Object getValueAt(int row, int column) {
    return "" + (row * column);

  public int getColumnCount() {
    return 4;

  public int getRowCount() {
    return 5;

class ImageRenderer extends DefaultTableCellRenderer {
  JLabel lbl = new JLabel();

  ImageIcon icon = new ImageIcon("HearthFlag.gif");

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    lbl.setText((String) value);
    return lbl;

Open in new window


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question