How can I render a JLabel with text and image in a JTable?

Scott Stanchfield

You need to define a custom renderer that sets the icon on the label

For example, first, define the renderer:

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MyRenderer extends DefaultTableCellRenderer {

  /*
   * @see TableCellRenderer#getTableCellRendererComponent(JTable, Object, boolean, boolean, int, int)
   */
  public Component getTableCellRendererComponent(JTable table, Object value,
                                                 boolean isSelected, boolean hasFocus, 
                                                 int row, int column) {
    ImageIcon icon = new ImageIcon(getClass().getResource("images/moon.gif"));
    setText((String)value);
    setIcon(icon);
    return this;
  }
}

Then, use the renderer in a table:

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class Sample {

  public static void main(String[] args) {
    JFrame f = new JFrame("Sample");
    f.getContentPane().setLayout(new GridLayout(1,0,0,0));
    TableModel model = new AbstractTableModel() {
      public Object getValueAt(int rowIndex, int columnIndex) {
        return rowIndex + "," + columnIndex;
      }
      public int getColumnCount() {
        return 3;
      }
      public int getRowCount() {
        return 10;
      }
    };

    JTable table = new JTable(model);
    table.getColumnModel().getColumn(1).setCellRenderer(new MyRenderer());
    JScrollPane pane = new JScrollPane(table);
    f.getContentPane().add(pane);
    f.pack();
    f.setVisible(true);
  }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact