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.

Create a Custom Cell Renderer in a JTable

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 % == 0) {
  cell.setBackground(Color.cyan);
  }
  else {
  cell.setBackground(Color.lightGray);
  }
  }
  return cell;
  }
  }
}

Download this example.

Output of program:

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java