Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

Custom Search
Swing Chapter 18. (Advanced topics) Tables. Easy for reading, Click here!

[ Return to Swing (Book) ]

Page: 2/9 



Previous Page Previous Page (1/9) - Next Page (3/9) Next Page
Subpages: 1. JTable  
2.
Stocks Table: part I - Basic JTable example 
3.
Stocks Table: part II - Custom renderers 
4. Stocks Table: part III - Data formatting 
5. Stocks Table: part IV - Sorting columns 
6.
Stocks Table: part V - JDBC
7.
Stocks Table: part VI - Column addition and removal
8.
Expense report application
9.
JavaBeans property editor

18.2  Stocks Table: part I - Basic JTable example

This basic example shows how to construct a JTable to display information about stock market data for a given day. Despite of its simplicity, it demonstrates the most fundamental features of JTable, and serves as a good basis for the more advanced examples that follow.

Stocks and stock trading is characterized by many attributes. The following are selected for display in our example:

Name                      Type                       Description

Symbol                   String                Stock's symbol (NYSE or NASDAQ)

Name                      String                Company name

Last                        double                Price at the end of the trade day

Open                      double                Price at the beginning of the trade day

Change                  double                Absolute change in price with respect to previous closing

Change %              double                Percent change in price with respect to previous closing

Volume                   long                     Day's volume of trade (in $) for this stock

Each stock attribute represents a column in our table, and each row represents a specific company's stock information.

Figure 18.1 JTable in a JScrollPane with 7 TableColumns and 16 rows of data.

<<file figure18-1.gif>>

The Code: StocksTable.java

see \Chapter18\1

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.io.*;

import java.text.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.event.*;

import javax.swing.table.*;

public class StocksTable extends JFrame

{

  protected JTable m_table;

  protected StockTableData m_data;

  protected JLabel m_title;

  public StocksTable() {

    super("Stocks Table");

    setSize(600, 340);

    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", 150, 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("04/06/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);

  }

}

Understanding the Code

Class StocksTable

This class extends JFrame to implement the frame container for our table. Three instance variables are declared (to be used extensively in more complex examples that follow):

JTable m_table: table component to display stock data.

StockTableData m_data: TableModel implementation to manage stock data (see below).

JLabel m_title: used to display stocks table title (date which stock prices are referenced).

The StocksTable constructor first initializes the parent frame object and builds an instance of StockTableData. Method getTitle()is invoked to set the text for the title label which is added to the northern region of the content pane. Then a JTable is created by passing the StockTableData instance to the constructor. Note the the autoCreateColumnsFromModel method is set to false because we plan on creating our own TableColumns.

As we will see below, the static array m_columns of the StockTableData class describes all columns of our table. It is used here to create each TableColumn instance and set their text alignment and width.

Method setHorizontalAlignment() (inherited by DefaultTableCellRenderer from JLabel) is used to set the proper alignment for each TableColumn's cell renderer. The TableColumn constructor takes a column index, width, and renderer as parameters. Note that TableCellEditor is set to null since we don't want to allow editing of stock data. Finally, columns are added to the table's TableColumnModel (which JTable created by default because we didn't specify one) with the addColumn() method.

In the next step, an instance of JTableHeader is created for this table, and the updateTableInRealTime property is set to false (this is done to demonstrate the effect this has on column dragging -- only a column's table header is displayed during a drag).

Lastly a JScrollPane instance is used to provide scrolling capabilities, and our table is added to its JViewport. This JScrollPane is then added to the center of our frame's content pane.

Class StockData

This class encapsulates a unit of stock data as described in the table above. The instance variables defined in this class have the following meaning:

String m_symbol: stock's symbol (NYSE or NASDAQ)

String m_name: company name

Double m_last: the price of the last trade

Double m_open: price at the beginning of the trade day

Double m_change: absolute change in price with respect to previous closing

Double m_changePr: percent change in price with respect to previous closing

Long m_volume: day's volume of trade (in $) for this stock

Note that all numerical data are encapsulated in Object-derived classes. This design decision simplifies data exchange with the table (as we will see below). The only constructor provided assigns each of these variables from the data passed as parameters.

Note: We use public instance variables in this and several other classes in this chapter to avoid overcomplication. In most professional apps these would either be protected or private.

Class ColumnData

This class encapsulates data describing the visual characteristics of a single TableColumn of our table. The instance variables defined in this class have the following meaning:

String m_title:    column title

int m_width: column width in pixels

int m_alignment: text alignment as defined in JLabel

The only constructor provided assigns each of these variables the data passed as parameters.

Class StockTableData

This class extends AbstractTableModel to serve as the data model for our table. Recall that AbstractTableModel is an abstract class, and three methods must be implemented to instantiate it:

public int getRowCount(): returns the number of rows in the table.

public int getColumnCount(): returns the number of columns in the table.

public Object getValueAt(int row, int column): returns data in the specified cell as an Object instance.

Note: An alternative approach is to extend the DefaultTableModel class which is a concrete implementation of AbstractTableModel. However, this is not recommended, as the few abstract methods in AbstractTableModel can be easily implemented. Usage of DefaultTableModel often creates unnecessary overhead.

By design, this class manages all information about our table, including the title and column data. A static array of ColumnData, m_columns, is provided to hold information about our table's columns (it is used in the StocksTable constructor, see above). Three instance variables have the following meaning:

SimpleDateFormat m_frm: used to format dates

Date m_date: date of currently stored market data

Vector m_vector: collection of StockData instances for each row in the table

The only constructor of the StockTableData class initializes two of these variables and calls the setDefaultData() method to assign the pre-defined default data to m_date and m_vector (in a later example we'll see how to use JDBC to retrieve data from a database rather than using hard-coded data as we do here).

As we discussed above, the getRowCount() and getColumnCount() methods should return the number of rows and columns, respectively. So their implementation is fairly obvious. The only catch is that they may be called by the AbstractTableModel constructor before any member variable is initialized. So we have to check for a null instance of m_vector. Note that m_columns, as a static variable, will be initialized before any non-static code is executed (so we don't have to check m_columns against null).

The remainder of the StockTableData class implements the following methods:

getColumnName(): returns the column title.

isCellEditable(): always returns false, because we want to disable all editing.

getValueAt(): retrieves data for a given cell as an Object. Depending on the column index, one of the StockData fields is returned.

getTitle(): returns our table's title as a String to be used in a JLabel in the northern region of our frame's content pane.

Running the Code

Figure 18.1 shows StocksTable in action displaying our hard-coded stock data. Note that the TableColumns  resize properly in response to the parent frame size. Also note that the selected row in our table can be moved by changed with the mouse or arrow keys, but no editing is allowed.



[ Return to Swing (Book) ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy