JTable myTable = new JTable(); TableCellRenderer myRenderer = myTable.getCellRenderer(0, 0);
public class CustomRenderer extends DefaultTableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); if (isSelected) { c.setBackground(Color.GREEN); } else { c.setBackground(Color.WHITE); } return c; } } JTable myTable = new JTable(data, headers); myTable.setDefaultRenderer(Object.class, new CustomRenderer());In this code, a class called CustomRenderer extends the DefaultTableCellRenderer class and overrides the getTableCellRendererComponent() method to customize the behavior of the cell renderer. Then a new JTable is created with some data and column headers, and the default renderer for Object.class is set to an instance of the CustomRenderer class. The javax.swing package library contains the JTable class, as well as many other classes for creating graphical user interfaces in Java.