Swing: JTable with JLabel and images

Posted on 2011-10-13
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
    LVL 47

    Expert Comment


    You need to use TableCellRendereere
    This is an example:

    Author Comment

    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

    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

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Using java code in .Net/c#: namespace issue 3 38
    hash value 2 29
    count7 challenge 12 52
    pairstar challenge 2 26
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now