Rendering Table Cells: To install the renderer as the default renderer for the JTable for Object.class : JTable Renderer Editor « Swing « Java Tutorial






Renderers for tables can be installed for individual classes or for specific columns.

TableCellRenderer renderer = new EvenOddRenderer();
table.setDefaultRenderer(Object.class, renderer);
Rendering Table Cells: To install the renderer as the default renderer for the JTable for Object.class
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

class EvenOddRenderer implements TableCellRenderer {

  public static final DefaultTableCellRenderer DEFAULT_RENDERER =
    new DefaultTableCellRenderer();

  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    Component renderer =
      DEFAULT_RENDERER.getTableCellRendererComponent(table, value,
      isSelected, hasFocus, row, column);
    Color foreground, background;
    if (isSelected) {
      foreground = Color.YELLOW;
      background = Color.GREEN;
    }  else {
      if (row % 2 == 0) {
        foreground = Color.BLUE;
        background = Color.WHITE;
      }  else {
        foreground = Color.WHITE;
        background = Color.BLUE;
      }
    }
    renderer.setForeground(foreground);
    renderer.setBackground(background);
    return renderer;
  }
}
public class ResizeTable {
  public static void main(String args[]) {

    final Object rowData[][] = { 
        { "1", "one",  "I" },
        { "2", "two",  "II" },
        { "3", "three", "III" }};
    final String columnNames[] = { "#", "English", "Roman" };

    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    table.setDefaultRenderer(Object.class,new EvenOddRenderer());    
    
    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);

  }
}








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.10.ColorChooserEditor
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