Create a Custom Cell Renderer in a JTable

After getting the knowledge about the JTable components
and creation with column header, you will be able to create a custom cell
renderer in a JTable component. Here, first of all you will know about the cell
renderer in JTable. The cell renderer is the component of JTable that can
be used to display the cells in a column. The JTable describes a specific
renderer to the particular column. For this the JTable invokes the table model
to use the getColumnClass method that takes the data types of the
column's cells.
The custom cell renderer means user created renderer
according to requirement. The given code with description is to provide
the facility for creating a custom cell renderer in a JTable to use some Java methods
and APIs.
Description of program:
In this program, you will see how to create a custom
cell renderer. To create a table cell renderer you need a JTable, so that
this program can create a table containing the data and column with column
header in the very first step. After that you will create a custom cell renderer
to apply the CustomTableCellRenderer() method by using the setCellRenderer
method that specify the cells in a particular column to use renderer. The
CustomTableCellRenderer method extends the DefaultTableCellRenderer
that helps you to create a default table cell renderer. Finally, it will create
a custom cell renderer that will display in given output of table. The even rows
of a JTable contains cyan color and odd rows contains lightGray
color but when you select it, the selected row or rows will look like green
color.
Description of code:
DefaultTableCellRenderer():
This is the constructor of DefaultTableCellRenderer class that can be
used for displaying the individual cells in a JTable. It constructs a default
table cell renderer.
getTableCellRendererComponent( JTable table, Object
obj, boolean isSelected, boolean hasFocus, int row, int column):
This is the method that returns default table cell renderer. It takes the
following agruments:
table:
This is a JTable.
obj: This is the value that is
assigning in the cells at the specified positions (rows and columns).
isSelected: When any cell is
selected then it becomesl true.
hasFocus: This the the
specific cell that is true when you focus any cell of a JTable.
row: This is the row of cell
renderer.
column: This is the column of
cell renderer.
isSelected():
This method returns the state of column either true or false. If any cell is
selected then it will return true otherwise false.
Here is the code of program:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class CustomCellRenderer{
JTable table;
TableColumn tcol;
public static void main(String[] args) {
new CustomCellRenderer();
}
public CustomCellRenderer(){
JFrame frame = new JFrame("Creating a Custom Cell Reanderer!");
JPanel panel = new JPanel();
String data[][] = {{"Vinod","Computer","3"},
{"Rahul","History","2"},
{"Manoj","Biology","4"},
{"Sanjay","PSD","5"}};
String col [] = {"Name","Course","Year"};
DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model);
tcol = table.getColumnModel().getColumn(0);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = table.getColumnModel().getColumn(1);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = table.getColumnModel().getColumn(2);
tcol.setCellRenderer(new CustomTableCellRenderer());
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table);
panel.add(pane);
frame.add(panel);
frame.setSize(500,150);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public class CustomTableCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.green);
}
else {
if (row % 2 == 0) {
cell.setBackground(Color.cyan);
}
else {
cell.setBackground(Color.lightGray);
}
}
return cell;
}
}
}
|
Download this example.
Output of program:

|
Current Comments
2 comments so far (post your own) View All Comments Latest 10 Comments:Through setSize() ,we are setting the size of the frame only and the Table remains as it is. Plz tell me the way by which i can adjust the size of Table also.
Thank you,
Posted by Ashish on Tuesday, 01.15.08 @ 16:29pm | #45257
this tut very helpful to me. and I want to align the jTable cell value in right. because when currency type value should apper with right alignment. so please If any one know it tell me.
Posted by indu on Monday, 04.23.07 @ 09:12am | #14786