Editing Table Cells: JComboBox As Table Cell Renderer : JTable Renderer Editor « Swing « Java Tutorial

Editing Table Cells: JComboBox As Table Cell Renderer
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

class ComboTableCellRenderer implements ListCellRenderer, TableCellRenderer {
  DefaultListCellRenderer listRenderer = new DefaultListCellRenderer();

  DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();

  private void configureRenderer(JLabel renderer, Object value) {
    if ((value != null) && (value instanceof Color)) {
    } else {
      renderer.setText((String) value);

  public Component getListCellRendererComponent(JList list, Object value, int index,
      boolean isSelected, boolean cellHasFocus) {
    listRenderer = (DefaultListCellRenderer) listRenderer.getListCellRendererComponent(list, value,
        index, isSelected, cellHasFocus);
    configureRenderer(listRenderer, value);
    return listRenderer;

  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    tableRenderer = (DefaultTableCellRenderer) tableRenderer.getTableCellRendererComponent(table,
        value, isSelected, hasFocus, row, column);
    configureRenderer(tableRenderer, value);
    return tableRenderer;

class ColorTableModel extends AbstractTableModel {

  Object rowData[][] = { { "1", Color.RED },
      { "2", Color.BLUE },
      { "3", Color.GREEN },
      { "4", Color.MAGENTA },
      { "5", Color.PINK }, };

  String columnNames[] = { "English","Color" };

  public int getColumnCount() {
    return columnNames.length;

  public String getColumnName(int column) {
    return columnNames[column];

  public int getRowCount() {
    return rowData.length;

  public Object getValueAt(int row, int column) {
    return rowData[row][column];

  public Class getColumnClass(int column) {
    return (getValueAt(0, column).getClass());

  public void setValueAt(Object value, int row, int column) {
    rowData[row][column] = value;

  public boolean isCellEditable(int row, int column) {
    return (column != 0);

public class EditableColorColumn {

  public static void main(String args[]) {
    Color choices[] = { Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE,
        Color.MAGENTA };
    ComboTableCellRenderer renderer = new ComboTableCellRenderer();
    JComboBox comboBox = new JComboBox(choices);
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("Editable Color Table");
    TableModel model = new ColorTableModel();
    JTable table = new JTable(model);
    TableColumn column = table.getColumnModel().getColumn(1);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);

14.60.JTable Renderer Editor
14.60.1.Get Default cell Renderer
14.60.2.Get Default Cell Editor
14.60.3.Default Editors and Renderers
14.60.4.Rendering Table Cells: To install the renderer as the default renderer for the JTable for Object.classRendering Table Cells: To install the renderer as the default renderer for the JTable for Object.class
14.60.5.implements TableCellRendererimplements TableCellRenderer
14.60.6.extends DefaultCellEditorextends DefaultCellEditor
14.60.7.Cell Renderer for the Gender ColumnCell Renderer for the Gender Column
14.60.8.Demonstrates the editable property of Swing tables.Demonstrates the editable property of Swing tables.
14.60.9.An Editor for the Gender ColumnAn Editor for the Gender Column
14.60.11.Using a JComboBox in a Cell in a JTable Component
14.60.12.Editing Table Cells: JComboBox As Table Cell RendererEditing Table Cells: JComboBox As Table Cell Renderer
14.60.13.Using a List JSpinner as a Cell Editor in a JTable Component
14.60.14.Using default Boolean value cell editor and rendererUsing default Boolean value cell editor and renderer
14.60.15.Creating a Class-Based Custom Cell Renderer in a JTable Component
14.60.16.Creating a Custom Cell Renderer in a JTable Component
14.60.17.Preventing Invalid Values in a Cell in a JTable Component
14.60.18.Using Built-In Cell Renderers and Editors in a JTable Component
14.60.19.Save the current value in the cell being edited and stops the editing process
14.60.20.Discard any changes made by the user and stops the editing process