Colored Table CellRenderer : Table Renderer Editor « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Java » Swing JFC » Table Renderer EditorScreenshots 
Colored Table CellRenderer
Colored Table CellRenderer
 
/*
Swing, Second Edition
by Matthew Robinson, Pavel Vorobiev

*/


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class StocksTable2 extends JFrame {
  protected JTable m_table;

  protected StockTableData m_data;

  protected JLabel m_title;

  public StocksTable2() {
    super("Stocks Table");
    setSize(600300);

    m_data = new StockTableData();

    m_title = new JLabel(m_data.getTitle()new ImageIcon("money.gif"),
        SwingConstants.LEFT);
    m_title.setFont(new Font("TimesRoman", Font.BOLD, 24));
    m_title.setForeground(Color.black);
    getContentPane().add(m_title, BorderLayout.NORTH);

    m_table = new JTable();
    m_table.setAutoCreateColumnsFromModel(false);
    m_table.setModel(m_data);

    for (int k = 0; k < StockTableData.m_columns.length; k++) {
      DefaultTableCellRenderer renderer = new ColoredTableCellRenderer();
      renderer
          .setHorizontalAlignment(StockTableData.m_columns[k].m_alignment);
      TableColumn column = new TableColumn(k,
          StockTableData.m_columns[k].m_width, renderer, null);
      m_table.addColumn(column);
    }

    JTableHeader header = m_table.getTableHeader();
    header.setUpdateTableInRealTime(false);

    JScrollPane ps = new JScrollPane();
    ps.getViewport().add(m_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 StocksTable2();
  }
}

class ColoredTableCellRenderer extends DefaultTableCellRenderer {
  public void setValue(Object value) {
    if (value instanceof ColorData) {
      ColorData cvalue = (ColorDatavalue;
      setForeground(cvalue.m_color);
      setText(cvalue.m_data.toString());
    else if (value instanceof IconData) {
      IconData ivalue = (IconDatavalue;
      setIcon(ivalue.m_icon);
      setText(ivalue.m_data.toString());
    else
      super.setValue(value);
  }
}

class ColorData {
  public Color m_color;

  public Object m_data;

  public static Color GREEN = new Color(01280);

  public static Color RED = Color.red;

  public ColorData(Color color, Object data) {
    m_color = color;
    m_data = data;
  }

  public ColorData(Double data) {
    m_color = data.doubleValue() >= ? GREEN : RED;
    m_data = data;
  }

  public String toString() {
    return m_data.toString();
  }
}

class IconData {
  public ImageIcon m_icon;

  public Object m_data;

  public IconData(ImageIcon icon, Object data) {
    m_icon = icon;
    m_data = data;
  }

  public String toString() {
    return m_data.toString();
  }
}

class StockData {
  public static ImageIcon ICON_UP = new ImageIcon("ArrUp.gif");

  public static ImageIcon ICON_DOWN = new ImageIcon("ArrDown.gif");

  public static ImageIcon ICON_BLANK = new ImageIcon("blank.gif");

  public IconData m_symbol;

  public String m_name;

  public Double m_last;

  public Double m_open;

  public ColorData m_change;

  public ColorData m_changePr;

  public Long m_volume;

  public StockData(String symbol, String name, double last, double open,
      double change, double changePr, long volume) {
    m_symbol = new IconData(getIcon(change), symbol);
    m_name = name;
    m_last = new Double(last);
    m_open = new Double(open);
    m_change = new ColorData(new Double(change));
    m_changePr = new ColorData(new Double(changePr));
    m_volume = new Long(volume);
  }

  public static ImageIcon getIcon(double change) {
    return (change > ? ICON_UP : (change < ? ICON_DOWN : ICON_BLANK));
  }
}

class ColumnData {
  public String m_title;

  public int m_width;

  public int m_alignment;

  public ColumnData(String title, int width, int alignment) {
    m_title = title;
    m_width = width;
    m_alignment = alignment;
  }
}

class StockTableData extends AbstractTableModel {
  static final public ColumnData m_columns[] {
      new ColumnData("Symbol"100, JLabel.LEFT),
      new ColumnData("Name"160, JLabel.LEFT),
      new ColumnData("Last"100, JLabel.RIGHT),
      new ColumnData("Open"100, JLabel.RIGHT),
      new ColumnData("Change"100, JLabel.RIGHT),
      new ColumnData("Change %"100, JLabel.RIGHT),
      new ColumnData("Volume"100, JLabel.RIGHT) };

  protected SimpleDateFormat m_frm;

  protected Vector m_vector;

  protected Date m_date;

  public StockTableData() {
    m_frm = new SimpleDateFormat("MM/dd/yyyy");
    m_vector = new Vector();
    setDefaultData();
  }

  public void setDefaultData() {
    try {
      m_date = m_frm.parse("4/6/1999");
    catch (java.text.ParseException ex) {
      m_date = null;
    }

    m_vector.removeAllElements();
    m_vector.addElement(new StockData("ORCL""Oracle Corp."23.6875,
        25.375, -1.6875, -6.4224976600));
    m_vector.addElement(new StockData("EGGS""Egghead.com"17.25,
        17.4375, -0.1875, -1.432146400));
    m_vector.addElement(new StockData("T""AT&T"65.187566, -0.8125,
        -0.10554000));
    m_vector.addElement(new StockData("LU""Lucent Technology"64.625,
        59.93754.68759.6529856300));
    m_vector.addElement(new StockData("FON""Sprint"104.5625106.375,
        -1.8125, -1.821135100));
    m_vector.addElement(new StockData("ENML""Enamelon Inc."4.8755,
        -0.125035900));
    m_vector.addElement(new StockData("CPQ""Compaq Computers"30.875,
        31.25, -0.375, -2.1811853900));
    m_vector.addElement(new StockData("MSFT""Microsoft Corp."94.0625,
        95.1875, -1.125, -0.9219836900));
    m_vector.addElement(new StockData("DELL""Dell Computers"46.1875,
        44.51.68756.2447310000));
    m_vector.addElement(new StockData("SUNW""Sun Microsystems"140.625,
        130.93751010.62517734600));
    m_vector.addElement(new StockData("IBM""Intl. Bus. Machines"183,
        183.125, -0.125, -0.514371400));
    m_vector.addElement(new StockData("HWP""Hewlett-Packard"70,
        71.0625, -1.4375, -2.012410700));
    m_vector.addElement(new StockData("UIS""Unisys Corp."28.2529,
        -0.75, -2.592576200));
    m_vector.addElement(new StockData("SNE""Sony Corp."96.187595.625,
        1.1251.18330600));
    m_vector.addElement(new StockData("NOVL""Novell Inc."24.0625,
        24.375, -0.3125, -3.026047900));
    m_vector.addElement(new StockData("HIT""Hitachi, Ltd."78.577.625,
        0.8751.1249400));
  }

  public int getRowCount() {
    return m_vector == null : m_vector.size();
  }

  public int getColumnCount() {
    return m_columns.length;
  }

  public String getColumnName(int column) {
    return m_columns[column].m_title;
  }

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

  public Object getValueAt(int nRow, int nCol) {
    if (nRow < || nRow >= getRowCount())
      return "";
    StockData row = (StockDatam_vector.elementAt(nRow);
    switch (nCol) {
    case 0:
      return row.m_symbol;
    case 1:
      return row.m_name;
    case 2:
      return row.m_last;
    case 3:
      return row.m_open;
    case 4:
      return row.m_change;
    case 5:
      return row.m_changePr;
    case 6:
      return row.m_volume;
    }
    return "";
  }

  public String getTitle() {
    if (m_date == null)
      return "Stock Quotes";
    return "Stock Quotes at " + m_frm.format(m_date);
  }
}

           
         
  
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.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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.