Stocks data Table: illustrate the TableModel : Table Model « Swing JFC « Java






Stocks data Table: illustrate the TableModel

Stocks data Table: illustrate the TableModel
     
/*
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 StocksTable extends JFrame {
  protected JTable m_table;

  protected StockTableData m_data;

  protected JLabel m_title;

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

    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 DefaultTableCellRenderer();
      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 StocksTable();
  }
}

class StockData {
  public String m_symbol;

  public String m_name;

  public Double m_last;

  public Double m_open;

  public Double m_change;

  public Double m_changePr;

  public Long m_volume;

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

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.42, 24976600));
    m_vector.addElement(new StockData("EGGS", "Egghead.com", 17.25,
        17.4375, -0.1875, -1.43, 2146400));
    m_vector.addElement(new StockData("T", "AT&T", 65.1875, 66, -0.8125,
        -0.10, 554000));
    m_vector.addElement(new StockData("LU", "Lucent Technology", 64.625,
        59.9375, 4.6875, 9.65, 29856300));
    m_vector.addElement(new StockData("FON", "Sprint", 104.5625, 106.375,
        -1.8125, -1.82, 1135100));
    m_vector.addElement(new StockData("ENML", "Enamelon Inc.", 4.875, 5,
        -0.125, 0, 35900));
    m_vector.addElement(new StockData("CPQ", "Compaq Computers", 30.875,
        31.25, -0.375, -2.18, 11853900));
    m_vector.addElement(new StockData("MSFT", "Microsoft Corp.", 94.0625,
        95.1875, -1.125, -0.92, 19836900));
    m_vector.addElement(new StockData("DELL", "Dell Computers", 46.1875,
        44.5, 1.6875, 6.24, 47310000));
    m_vector.addElement(new StockData("SUNW", "Sun Microsystems", 140.625,
        130.9375, 10, 10.625, 17734600));
    m_vector.addElement(new StockData("IBM", "Intl. Bus. Machines", 183,
        183.125, -0.125, -0.51, 4371400));
    m_vector.addElement(new StockData("HWP", "Hewlett-Packard", 70,
        71.0625, -1.4375, -2.01, 2410700));
    m_vector.addElement(new StockData("UIS", "Unisys Corp.", 28.25, 29,
        -0.75, -2.59, 2576200));
    m_vector.addElement(new StockData("SNE", "Sony Corp.", 96.1875, 95.625,
        1.125, 1.18, 330600));
    m_vector.addElement(new StockData("NOVL", "Novell Inc.", 24.0625,
        24.375, -0.3125, -3.02, 6047900));
    m_vector.addElement(new StockData("HIT", "Hitachi, Ltd.", 78.5, 77.625,
        0.875, 1.12, 49400));
  }

  public int getRowCount() {
    return m_vector == null ? 0 : 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 < 0 || nRow >= getRowCount())
      return "";
    StockData row = (StockData) m_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.Append a row to a table through DefaultTableModel at specified row
2.Remove the first row from a table with DefaultTableModel
3.Remove the last row from a table with DefaultTableModel
4.Move the first row to the end of the table
5.Move the last row to the beginning of the table
6.Move the first two rows to the end of the table
7.Move the last two rows to the start of the table
8.Get all the table data from DefaultTableModel
9.Copy (clone) the data from the second row
10.Overwrite the date from the first row with DefaultTableModel
11.Copy data from a table to a list
12.Insert a new column to a table
13.JTable class using default table models and a convenienceJTable class using default table models and a convenience
14.extends DefaultTableModel to create your own table model and build table from thatextends DefaultTableModel to create your own table model and build table from that
15.Table with a custom TableModelTable with a custom TableModel
16.extends AbstractTableModel to create custom modelextends AbstractTableModel to create custom model
17.Table model is based on call backTable model is based on call back
18.Hard code data in array for TableModelHard code data in array for TableModel
19.A JTable class using default table models and a convenience constructorA JTable class using default table models and a convenience constructor
20.Custom model, POJO and JTableCustom model, POJO and JTable
21.Custom table model, File data based ModelCustom table model, File data based Model
22.Paging or pagable JTable(Table) Model for large data setPaging or pagable JTable(Table) Model for large data set
23.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.Paging JTable(Table) Model with an input field for dynamically altering the size of a page.
24.File data Table: file name, size, type File data Table: file name, size, type
25.AbstractTableModel backed by HashtableAbstractTableModel backed by Hashtable
26.Fixed data vs dynamic data TableFixed data vs dynamic data Table
27.Use model to control the Editable ColumnsUse model to control the Editable Columns
28.Converts a visible column index to a column index in the model.
29.Converts a column index in the model to a visible column index
30.Returns the visible columns in the order that they appear in the model
31.Appending a Column to a JTable Component using DefaultTableModel
32.Add a column with values.
33.Disable autoCreateColumnsFromModel
34.Add a column without affecting existing columns
35.Remove the first visible column without removing the underlying data
36.Sharing a Table Model Between JTable Components
37.Creating simple JTable using AbstractTableModel
38.Map TableModel
39.TableSorter extends AbstractTableModel
40.TableSorter is a decorator for TableModels
41.Bean Property Table Model
42.A simple extension of JTable that supports the use of a SortableTableModel.
43.This program shows how to build a table from a table modelThis program shows how to build a table from a table model