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

AWTJavaObject Oriented ProgrammingProgrammingSwing

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 two ways : By class of the object to be rendered using table.setDefaultRenderer() method and by a column using tableColumn.setCellRenderer() method and tableColumn.setHeaderRenderer() method for a specific column headers.
  • The TableCellRenderer interface has only one method getTableCellRendererComponent() and this method can return different rendering components based on the value, a cell has the focus or is selected, row and column that can contain the value.

TableCellEditor

  • A TableCellEditor is an interface and by default, the cells can be editable.
  • A TableCellEditor can be determined by calling isCellEditable() method of a TableModel.
  • If the class of a cell value is Boolean, a JCheckBox can be used. If must double click to put in edit mode, a JTextField can be used.
  • The TableCellEditor interface has only one method getTableCellEditorComponent() and this method can return different editing components based on the value, a cell is selected, row and column that can contain the value.

Example

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

raja
Published on 18-Jul-2019 17:07:33
Advertisements