Friday, November 28, 2008

Show Image In JTable

In this short post I present, how can we put and show images in JTable component.

Problem
Sometimes we need to put and show images in JTable. A typical situation is when we have a boolean true or false and we don't want to write true/false or yes/no captions.

Solution
The solution is very simply. We have to override the
getColumnClass(int columnIndex)

method of TableModel class, like this:

DefaultTableModel model = new DefaultTableModel() {
@Override
public Class getColumnClass(int columnIndex) {
//Set the index of the column, where the images will be.
final int imageIndex = 5;
return columnIndex == imageIndex ? ImageIcon.class : Object.class;
}
};

I hope it will be useful.

No comments: