Adding JLabel in JTable
Hello guys,
This post is related to the Swing of Java. Mostly in JTable we can see that we need the icons in a table for the reliability and RIA of software for the front-end user. So to use icon we used to import the .png/.jpg/.gif in our project. Firstly you need to know how the JTable cell are being rendered and we need to know the model of the JTable.
You can refer to the api:
Ok now let’s move to the main Job.
We need to know which one column is need to place an icon. Futher more the table must be made from our own custom Table Model. Not only that we need to add a table cell renderer . Our main motto is to add a JLabel in a Cell of JTable. After you have referred to the API it would be easier to deal with the cell renderer. This cell renderer can be obtained by extending DefaultTableCellRenderer.
Source Code for CellRenderer:
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class Renderer extends DefaultTableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
if(value instanceof JLabel){
//This time return only the JLabel without icon
return (JLabel)value;
}
else
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
In the same way i’ll create a extended DefaultTableModel
import javax.swing.JLabel;
class MyModel extends javax.swing.table.DefaultTableModel{
Object[][] row = {{new JLabel("Row 1 Col 1"), "Row 1 Col 2", "Row 1 Col3"},
{new JLabel("Row 2 Col 1"), "Row 2 Col 2", "Row 2 Col3"},
{new JLabel("Row 3 Col 1"), "Row 3 Col 2", "Row 3 Col3"},
{new JLabel("Row 4 Col 1"), "Row 4 Col 2", "Row 4 Col3"}};
Object[] col = {"Column 1", "Column 2", "Column 3"};
public MyModel (){
//Adding columns
for(Object c: col)
this.addColumn(c);
//Adding rows
for(Object[] r: row)
addRow(r);
}
@Override
public Class getColumnClass(int columnIndex) {
if(columnIndex == 0)return getValueAt(0, columnIndex).getClass();
else return super.getColumnClass(columnIndex);
}
}
Now we’ll create a small app extending JFrame and utilizing the above classes.
import javax.swing.*;
import ftp.gui.IconTableCellRenderer;
public class MyTableTest extends JFrame{
public MyTableTest(String title){
super(title);
showGUI();
}
public void showGUI(){
JTable table = new JTable();
table.setModel(new MyModel());//invoking our custom model
table.setDefaultRenderer(JLabel.class, new Renderer());// for the rendering of cell
JScrollPane jp = new JScrollPane(table);
getContentPane().add(jp);
setVisible(true);
setSize(500,300);
}
public static void main(String[] args) {
MyTableTest t = new MyTableTest("Table Custom");
}
}
After all the output must be like this:
To make the JLabel selected as other cell of JTable.
For this you need to add some more code in Cell Renderer.
Modified Cell Renderer for selection of JLabel
import javax.swing.JLabel;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class Renderer extends DefaultTableCellRenderer{
public void fillColor(JTable t,JLabel l,boolean isSelected ){
//setting the background and foreground when JLabel is selected
if(isSelected){
l.setBackground(t.getSelectionBackground());
l.setForeground(t.getSelectionForeground());
}
else{
l.setBackground(t.getBackground());
l.setForeground(t.getForeground());
}
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
if(value instanceof JLabel){
JLabel label = (JLabel)value;
//to make label foreground n background visible you need to
// setOpaque -> true
label.setOpaque(true);
fillColor(table,label,isSelected);
return label;
}
else
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
Now the output must be something like this:
To add Icon on JTable Cell
For adding icon on JTable you can simply add a line of code in your Renderer.
I’ve just added one line code in the above DefaultTableCellRender extended class i.e. Renderer
//In the method of getTableCellRendererComponent
if(value instanceof JLabel){
JLabel label = (JLabel)value;
//you can add the image here
label.setIcon(new ImageIcon(getClass().getResource("folder.png")));
label.setOpaque(true);
fillColor(table,label,isSelected);
return label;
}
//other stuff
Output:
That’s all for today.
Thanks you and Don’t hasitate on giving comment.




thanks. You saved me from googling for hours.
minor fix: listing 2 line 10. replace TableTest with MyModel
Way too complicated. This way is simpler:
For a given column:
TableColumn someColumn = table.getColumnModel().getColumn(0);
someColumn.setCellRenderer(IconifiedRenderer(new ImageIcon(getClass().getResource("/images/ic.png"))) );
//file: IconifiedRenderer.java
public class IconifiedRenderer extends DefaultTableCellRenderer {
Icon ic = null;
public IconifiedRenderer(Icon ic) {
this.ic = ic;
}
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setIcon(ic);
return this;
}
}
hi wang,
Ya it’s good and simple for icon cell !!But lil bit mistake..
TableColumn someColumn = table.getColumnModel().getColumn(0);
someColumn.setCellRenderer(new IconifiedRenderer(new ImageIcon(getClass().getResource(“/images/ic.png”))) );
Thanks..for comment !
completely wrong approach: never-ever add a component to the model! Instead (as already suggested) store the icon.
hi Kleopatra,
The icon is stored as a resource where it needs to be. Every Table Model has the getTableCellRendererComponent and I think you probably thinking why I’m using new ImageIcon everytime,After all it’s just a sample example which don’t cares of performance.
How about adding a tooltip to th icon? How to achieve that here?
GREAT HELP
THANKS
you are welcome