Change Table cell background with column renderer : Table Renderer Editor « Swing JFC « Java






Change Table cell background with column renderer

Change Table cell background with column renderer
  

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class ColorTableColumnRenderer extends JFrame{
  protected JTable table;

  protected MyTableModel tableModel;

  public ColorTableColumnRenderer() {
    super("Stocks Table");
    setSize(300, 300);

    tableModel = new MyTableModel();

    table = new JTable();
    table.setModel(tableModel);

    DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
    TableColumn column = new TableColumn(0,190, renderer, null);
    table.addColumn(column);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(table);
    getContentPane().add(ps, BorderLayout.CENTER);

    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    addWindowListener(wndCloser);
    setVisible(true);
  }

  public static void main(String argv[]) {
    new ColorTableColumnRenderer();
  }
}

class MyTableModel extends AbstractTableModel {
  protected Vector vector;

  protected int columnsCount = 1;

  public MyTableModel() {
    vector = new Vector();
    vector.addElement("2146400");
    vector.addElement("554000");
    vector.addElement("24230");
  }

  public int getRowCount() {
    return vector == null ? 0 : vector.size();
  }

  public int getColumnCount() {
    return columnsCount;
  }

  public String getColumnName(int column) {
    return "data";
  }

  public boolean isCellEditable(int nRow, int nCol) {
    return false;
  }

  public Object getValueAt(int nRow, int nCol) {
    if (nRow < 0 || nRow >= getRowCount())
      return "";
    return vector.elementAt(nRow);
  }

  public String getTitle() {
    return "data ";
  }

}

class ColoredTableCellRenderer extends DefaultTableCellRenderer {
  public void setValue(Object value) {
    setForeground(Color.red);
    super.setValue(value);
  }
}


           
         
    
  








Related examples in the same category

1.Shading Rows and Columns in a JTable Component
2.Shades every other column yellow
3.Using Built-In Cell Renderers and Editors in a JTable Component
4.Get Default cell Renderer
5.Get Default Cell Editor
6.Creating a Custom Cell Renderer in a JTable Component
7.Creating a Class-Based Custom Cell Renderer in a JTable Component
8.A slider renderer for volume values in a tableA slider renderer for volume values in a table
9.Table Cell Editor: ComboBoxCellEditor
10.Table with a custom cell renderer and editor for the color dataTable with a custom cell renderer and editor for the color data
11.Table with initialized column sizes and a combo box editorTable with initialized column sizes and a combo box editor
12.StockTable 3: CellRendererStockTable 3: CellRenderer
13.Colored Table CellRendererColored Table CellRenderer
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.