Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click here for MCI WorldCom.
Click here for MCI WorldCom.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Chapter 12
Table Component

In This Chapter
•  Table Elements
•  A Simple Table
•  Table Selection Models
•  Column Creation and Resizing Modes
•  Models and Support Classes Used by the JTable
•  Scrolling a Table

The JTable component is the most complex visual component in the JFC toolkit. During the review phases of the toolkit, the JTable class underwent the most API changes before they were stabilized to those contained in the current JTable class. To effectively use the JTable class in your applications, you must understand what the component is and is not designed to do. First, it is not a spreadsheet. However, you could have it view a spreadsheet data model. The JTable class is designed to be a high performance view of tabular data. The data is contained in a data model separate from the view. The columns in the table can be in a different order than they are in the data model. Not all columns in the data model need be displayed in the table. The table can be configured to allow selection of rows, columns, both rows and columns, or single cells.

This chapter will lead you through the complexities of using the JTable class. You will start with simply displaying tabular data in a table and continue to highly customize the table’s appearance.

You will learn how to

  Display tabular data in a table
  Use the TableModel interface and other support classes and interfaces in the Table package
  Control selection in a table
  Add renderers and editors to a table
  Customize the table’s headers

Table Elements

The JTable class delegates a great deal of functionality to support classes located in the swing.table package. A typical table is shown in Figure 12.1. In this figure, the various delegates are shown. The headers are displayed in an instance of the JTableHeader class. Each column is an instance of the TableColumn class that is managed by an instance of the TableColumnModel class. Selection is delegated to one or more ListSelectionModel instances. Finally, the data displayed in the table is encapsulated in a TableModel. A TableCellEditor is shown editing color values in the figure. A TableCellRenderer is used to paint the cells that are not being edited. Using and customizing these classes are the subjects of the remainder of this chapter.


Figure 12.1  The JTable class delegates.

A Simple Table

The JTable and its associated classes and interfaces provide a wealth of features and customization possibilities. However, sometimes you just want to present tabular data in a table. The JTable class contains methods for easily performing simple tasks as well as the complex issues covered later in this chapter.

The JTable class contains a pair of constructors for creating a table from simple Java collections. The first takes a two-dimensional array as its first parameter, the second takes a Vector of Vectors. As its second parameter, each method requires that the column names for the table be stored in the same class of collection as the data. The SimpleTableTest application shown in Listing 12.1 creates a table by passing a Vector containing the data for the table and another Vector containing the column names to the JTable constructor. The table created by the application is shown in Figure 12.2.


Figure 12.2  The SimpleTableTest application.

The Vector containing the data is a Vector of Vectors. Each Vector contained by the wrapping vector represents one row in the table. The default technique to view each object in the table is to call its toString method. This is appropriate for many types of objects, but not all. For example, columns two and three in this table could be better represented by using the actual color for column two and a check box for column three. Later in this chapter you will see how this can be accomplished by setting renderers for the table.

Listing 12.1 The SimpleTableTest Application

package com.foley.test;

import java.awt.*;

import java.util.Vector;

import javax.swing.*;
import javax.swing.border.*;

import com.foley.utility.ApplicationFrame;

/**
 * An application that displays a JTable instance
 * in its frame.
 *
 * @author Mike Foley
 **/
public class SimpleTableTest extends Object {

    /**
     * Application entry point.
     * Create a frame, the table and display it.
     *
     * @param args Command line parameter. Not used.
     **/
    public static void main( String args[] ) {

        JFrame frame = new ApplicationFrame( “SimpleTableTest Vector” );
        Vector data = new Vector();
        Vector row = new Vector();
        row.addElement( “Mike” );
        row.addElement( Color.blue );
        row.addElement( new Boolean( true ) );
        data.addElement( row );

        row = new Vector();
        row.addElement( “Mari” );
        row.addElement( Color.red );
        row.addElement( new Boolean( true ) );
        data.addElement( row );

        row = new Vector();
        row.addElement( “Molly” );
        row.addElement( Color.yellow );
        row.addElement( new Boolean( false ) );
        data.addElement( row );

        Vector columnNames = new Vector();
        columnNames.addElement( “Name” );
        columnNames.addElement( “Color” );
        columnNames.addElement( “Boolean” );
        JTable table = new JTable( data, columnNames );

        table.setBorder( BorderFactory.createLoweredBevelBorder() );

        frame.getContentPane().add( table, BorderLayout.CENTER );

        frame.pack();
        frame.setVisible( true );

    } // main

} // SimpleTableTest


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.