What are the differences between the TableCellRenderer and TableCellEditor in Java?



In this article, we will learn about the differences between the TableCellRenderer and TableCellEditor in Java. The JTable interface in Java Swing has these important interfaces called TableCellRenderer and TableCellEditor. Though they serve different purposes, they complement each other in defining the table cells' render and edit functions.

TableCellRenderer

A TableCellRenderer creates a component that displays the value of a JTable cell. The default renderer uses JLabel to display the value of each table cell.

The TableCellRenderer interface can be specified in three ways :

  • By the class of the object to be rendered using the table.setDefaultRenderer() method.
  • By a column using the tableColumn.setCellRenderer() method.
  • tableColumn.setHeaderRenderer() method for specific column headers.

Syntax

The following is the syntax for the TableCellRenderer Declaration:

public interface TableCellRenderer

The TableCellRenderer interface has only one method, getTableCellRendererComponent(), and this method can return different rendering components based on the value, whether a cell has the focus or is selected, and the row and column that can contain the value.

Method Declaration:

Component getTableCellRendererComponent(JTable table,
                                        Object value,
                                        boolean isSelected,
                                        boolean hasFocus,
                                        int row,
                                        int column);

When to Use TableCellRenderer

We can use the TableCellRenderer when:

  • We need to format text/colors (e.g., highlight negative numbers).
  • We want to display icons or custom components (e.g., a JProgressBar in a cell).

TableCellEditor

A TableCellEditor is an interface, and by default, the cells can be editable. A TableCellEditor can be determined by calling the isCellEditable() method of a TableModel. If the class of a cell value is Boolean, a JCheckBox can be used. We must double click to put it in edit mode, a JTextField can be used.

Syntax

The following is the syntax for the TableCellEditor Declaration:

public interface TableCellEditor extends CellEditor

The TableCellEditor interface has only one method, getTableCellEditorComponent(), and this method can return different editing components based on the value, the cell is selected, and the row and column that can contain the value.

Method Declaration:

Component getTableCellEditorComponent(JTable table,
                                      Object value,
                                      boolean isSelected,
                                      int row,
                                      int column);

When to Use TableCellEditor

We can use the TableCellEditor when:

  • We need custom input controls (e.g., JComboBox for dropdown selections).
  • We want to validate user input (e.g., restrict to numbers only).

Difference Table

The following are the key differences between the TableCellRenderer and TableCellEditor in Java:

Criteria TableCellRenderer TableCellEditor
Role Displays cell content Edit cell content
Trigger Automatically triggers during rendering Triggers on user interaction (e.g., mouse click)
Default Implementation DefaultTableCellRenderer DefaultCellEditor (wraps JTextField, JCheckBox, etc.)
Method to Implement getTableCellRendererComponent() getTableCellEditorComponent()
Return Type Component (e.g., JLabel) Component (e.g., JTextField)

Example of TableCellRenderer & TableCellEditor

Below is an example of TableCellRenderer and TableCellEditor in the TableCellRendererEditorTest class in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableCellRendererEditorTest extends JFrame {
   private JTable table;
   public TableCellRendererEditorTest() {
      setTitle("TableCellRendererEditor Test");
      DefaultTableModel dtm = new DefaultTableModel() {
         public boolean isCellEditable(int row, int column) {
            return !(column == 0);
         }
      };
      dtm.setDataVector(new Object[][]{{"Table Cell Renderer", "Table Cell Editor"}, 
      {"Table Cell Renderer","Table Cell Editor"}}, new Object[]{"Renderer","Editor"});
      table = new JTable(dtm);
      table.getColumn("Editor").setCellRenderer(new TextAreaRenderer());
      table.getColumn("Editor").setCellEditor(new TextAreaEditor());
      table.setRowHeight(80);
      JScrollPane spane = new JScrollPane(table);
      add(spane);
      setSize(400, 275);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new TableCellRendererEditorTest();
   }
}
class TextAreaRenderer extends JScrollPane implements TableCellRenderer {
   JTextArea textarea;
   public TextAreaRenderer() {
      textarea = new JTextArea();
      textarea.setLineWrap(true);
      textarea.setWrapStyleWord(true);
      getViewport().add(textarea);
   }
   public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column) {
      if (isSelected) {
         setForeground(table.getSelectionForeground());
         setBackground(table.getSelectionBackground());
         textarea.setForeground(table.getSelectionForeground());
         textarea.setBackground(table.getSelectionBackground());
      } else {
         setForeground(table.getForeground());
         setBackground(table.getBackground());
         textarea.setForeground(table.getForeground());
         textarea.setBackground(table.getBackground());
      }
      textarea.setText((String) value);
      textarea.setCaretPosition(0);
      return this;
   }
}
class TextAreaEditor extends DefaultCellEditor {
   protected JScrollPane scrollpane;
   protected JTextArea textarea;
   public TextAreaEditor() {
      super(new JCheckBox());
      scrollpane = new JScrollPane();
      textarea = new JTextArea();
      textarea.setLineWrap(true);
      textarea.setWrapStyleWord(true);
      scrollpane.getViewport().add(textarea);
   }
   public Component getTableCellEditorComponent
(JTable table, Object value, boolean isSelected, int row, int column) {
      textarea.setText((String) value);
      return scrollpane;
   }
   public Object getCellEditorValue() {
      return textarea.getText();
   }
}

Output

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-22T18:28:43+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements