Shading Rows and Columns in a JTable Component : Table Renderer Editor « Swing JFC « Java






Shading Rows and Columns in a JTable Component

 

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class Main {
  public static void main(String[] argv) {
    JTable table = new JTable() {
      public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) {
        Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
        if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
          c.setBackground(Color.yellow);
        } else {
          c.setBackground(getBackground());
        }
        return c;
      }
    };
  }
}

   
  








Related examples in the same category

1.Shades every other column yellow
2.Using Built-In Cell Renderers and Editors in a JTable Component
3.Get Default cell Renderer
4.Get Default Cell Editor
5.Creating a Custom Cell Renderer in a JTable Component
6.Creating a Class-Based Custom Cell Renderer in a JTable Component
7.A slider renderer for volume values in a tableA slider renderer for volume values in a table
8.Table Cell Editor: ComboBoxCellEditor
9.Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
10.Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
11.StockTable 3: CellRendererStockTable 3: CellRenderer
12.Colored Table CellRendererColored Table CellRenderer
13.Change Table cell background with column renderer Change Table cell background with column renderer
14.Install different Table Renderer for even and odd rowsInstall different Table Renderer for even and odd rows
15.A table that allows the user to pick a color from a pulldown listA table that allows the user to pick a color from a pulldown list
16.MixerModel and sliders for rendering volume valuesMixerModel and sliders for rendering volume values
17.Table Editor: Editable Color ColumnTable Editor: Editable Color Column
18.Tab in and out JComboBox in JTable
19.Creating a Custom Table Cell Editor in a JTable Component
20.Preventing Invalid Values in a Cell in a JTable Component
21.Setting the Activation Click Count for a Table Cell Editor in a JTable Component
22.Using a JComboBox in a Cell in a JTable Component
23.Using a List JSpinner as a Cell Editor in a JTable Component
24.Rendering an image in a JTable column
25.This program demonstrates cell rendering and editing in a table.This program demonstrates cell rendering and editing in a table.