JClass PageLayout

PreviousNextIndex

4

Creating Tables

Overview  Using JCPageTable  Table Structure  Creating a Table  Adding Data to Tables

Customizing Tables  Customizing Cells  Table Wrapping  Converting Tables

Tables in JClass PageLayout are implemented using the JCPageTable class. Tables contain column objects and row objects; the row objects contain the cells of the table. Table content is stored either in JCFrame objects belonging to individual cells or as objects in the entries of a TableDataModel associated with the table. Tables are printed by the flow, which breaks the table into rectangular ranges which are fit into flow frames.


4.1 Overview

The JCPageTable class provides methods and attributes for printing tables from your Java application. This chapter shows you how to:


4.2 Table Structure

A table is created with a given number of columns, and a column object is created for each. The number of rows in a table is not fixed, and row objects are added to the table either explicitly, by using a method such as JCPageTable.addRow(), or implicitly, through reference to cell objects beyond the current last row of the table. The table's rowList attribute stores all the rows created within the table.

Cells of the table are stored in lists associated with particular rows. A row object with no cells containing content will have a cell list with no entries. As cells are created, the cell object will be stored at the correct position in the row's cellList. As with the table's rowList, blank (null) entries are created for non-existent cells. The content of a cell can be stored in a JCFrame object allocated by the cell; as a standard JCFrame, it can contain any content that is valid for a JCFrame object, up to and including another JCPageTable.

In addition to ordered lists of row and column objects, the table object can also (conditionally) own another JCPageTable object. The secondary JCPageTable object defines the headers of the parent table, but a header JCPageTable cannot own a header table. The structure of a header JCPageTable is identical to the structure of a main table, except that the column layout of the header table is defined implicitly by reference to the parent table.

There are also dummy cell objects attached to the table, and to each row and column object. These cell objects store column-, row-, and table-wide default cell attributes.


4.3 Using JCPageTable

Here is an overview of the general steps required to use JCPageTable.

  1. Create a JCPageTable object and pass in:
    • the document to which this table belongs
    • the number of columns in this table
    • column widths
  2. Create table header by calling createHeaders(), if desired.
  3. Customize cells by setting borders and margins on table, rows, columns, or individual cells and by setting up any cell spans that are required.
  4. Size table to frame using fitToFrame(), if desired.
  5. Add data to tables (for example, call JCPageTable.printToCell() or JCPageTable.addRow()).
  6. Print the table by calling flow.print(table), where flow is an instance of JCFlow.

4.4 Creating a Table

By the end of this chapter, we will produce the following table:

Figure 3 :  A Table example in JClass PageLayout.

To begin, we define a table with three columns, each of which is one inch wide. Here is the relevant code.

  JCPageTable table = new JCPageTable(document, 3,
    new JCUnit.Measure(JCUnit.INCHES, 1.0));

The JCPageTable parameters indicate the JCDocument to which this table belongs and that this table should contain three columns, each one inch wide.


4.5 Adding Data to Tables

To print data into cells, use the JCPageTable.printToCell() method. It prints text to the cells you specify, generating them if necessary. For instance, the following code prints the text "Calico", using the current JCTextStyle (the style in the code sample below), to the cell found in row 2, column 1.

  table.printToCell(2, 1, style, "Calico");

Note: Cell indices start at (0, 0), independent of the presence of a header row.

4.5.1 Creating Body Rows

When you print to a cell frame that does not yet exist, you create a row of cells that correspond to the number of columns in the table. The following example populates the first three body rows of the table shown in Figure 3.

    table.printToCell(0, 0, style, "Labrador");
    table.printToCell(0, 1, style, "Persian");
    table.printToCell(0, 2, style, "Shetland");
    table.printToCell(1, 0, style, "Collie");
    table.printToCell(1, 1, style, "Siamese");
    table.printToCell(1, 2, style, "Arabian");
    table.printToCell(2, 0, style, "Terrier");
    table.printToCell(2, 1, style, "Calico");
    table.printToCell(2, 2, style, "Clydesdale");

To actually print the table, call

  flow.print(table);

where flow is an instance of JCFlow.

Note that JClass PageLayout identifies each cell by numbering cells in a row, column format, starting from zero (0).

Note that if a row cannot fit in the current page, then that row will be placed on the following page. However, if the height of that row is larger than the height of the entire page (for example, in the case that the row contains particularly extensive data, such as a long String), then that row is truncated.

The table is beginning to take shape:

Figure 4 :  Table with body rows.

4.5.2 Adding Body Rows

You can also add rows of cells to the end of a table using JCPageTable.addRow().

  table.addRow(style, newString[]{"Setter", "Tabby", "Palomino"});

The example produces the following results:

Figure 5 :  Table with additional body row.

4.5.3 Adding Header Rows

Suppose you wanted to create a table header row. Although a header row may look as though it is part of the table containing the body rows, in actuality it is a separate table unto itself. You build the header table using JCPageTable.createHeaders(). Once you have created the header table, you can populate its cells, much as you did the body rows.

  JCPageTable header = table.createHeaders();
  try {
    JCFrame frame = header.getCellFrame(0, 0);
    frame.print(style, "Dogs");
    
    frame = header.getCellFrame(0, 1);
    frame.print(style, "Cats");

    frame = header.getCellFrame(0, 2);
    frame.print(style, "Horses");
  }
  catch (EndOfFrameException e) {}

Adding the header row produces the following results:

Figure 6 :  Table with header row.


4.6 Customizing Tables

Now that you've laid out your table and printed data into it, you may want to customize its appearance by defining borders and background colors or by defining various line styles and shadings. This section describes how.

4.6.1 Table Styles

The simplest way to customize a table is to choose one of the numerous built-in table styles in JCTableStyle. Table styles are constants, such as JCTableStyle.STYLE_DEFAULT, or JCTableStyle.STYLE_n, where n ranges from 1 to 17.

Table styles are cloneable. You can further customize a table style by cloning the one you wish to use as a base for making changes.

  // Create a table style with orange headers and alternating colored
  // rows

  JCTableStyle tableStyle =
      (JCTableStyle)JCTableStyle.STYLE_DEFAULT.clone();

  tableStyle.getHeaderStyle().setBackground(Color.orange);
  tableStyle.setAlternate( new JCAlternate(Color.lightGray,
      Color.white, true));

Style Name

Description

Image

Default

  • thin left, right, top, bottom, header, horizontal, and column borders
  • no column shading
  • regular heading font
  • Style0

  • no left, right, top, bottom, header, horizontal, or column borders
  • no column shading
  • regular heading font
  • Style 1

  • thick header border; no other borders
  • no column shading
  • regular heading font
  • Style 2

  • thin header border; thick top and bottom borders; no other borders
  • no column shading
  • regular heading font
  • Style 3

  • thick top, bottom, and header borders; thin horizontal borders; no left, right, or column borders
  • no column shading
  • regular heading font
  • Style 4

  • no left, right, top, bottom, horizontal, or column borders; thick header border
  • header colored (black); reverse type for header text
  • no column shading
  • Style 5

  • thick right, left, top, bottom, and header borders; no column or horizontal borders
  • header colored (black); reverse type for header text
  • no column shading
  • Style 6

  • thick right, left, bottom and header borders; thin horizontal and column borders; no top border
  • header colored (black); reverse type for header text
  • no column shading
  • Style 7

  • thick right, left, top, bottom and header borders; no horizontal or column borders
  • header colored (gray); reverse type for header text
  • no column shading
  • Style 8

  • thick right, left, top, bottom, and header borders; thin horizontal and column borders
  • header colored (gray); reverse type for header text
  • no column shading
  • Style 9

  • thin header border; thick top, and bottom borders; no right, left, horizontal, and column borders
  • header colored (gray); reverse type for header text
  • no column shading
  • Style 10

  • thick right, left, top, and bottom borders; thin header, horizontal, and column borders
  • regular heading font
  • no column shading
  • Style 11

  • no right, left, top, and bottom borders; thin header, horizontal, and column borders
  • plain headers
  • no column shading
  • Style 12

  • thick right, left, top, bottom, and header borders; no horizontal or column borders
  • header colored (gray); reverse type for header text
  • gray column shading
  • Style 13

  • thick right, left, bottom, and header borders; thin horizontal border; no top or column borders
  • header colored (black); reverse type for header text
  • no column shading
  • Style 14

  • thin right, left, top, bottom, and horizontal borders; thick header border; no column border
  • plain header
  • no column shading
  • Style 15

  • thin right, left, top, bottom, header, and column borders; no horizontal border
  • plain header
  • no column shading
  • Style 16

  • thin top, bottom, right, left and horizontal borders; thick header border; thin border between last two columns
  • header colored (dark gray); reverse type for header text
  • no column shading
  • alternate row shading (light gray and dark gray)
  • Style 17

  • thin top, bottom, right, left, and horizontal borders; thick header border; thin border between last two columns
  • header reverse type
  • alternate column shading (light gray and dark gray)
  • JCTableStyle Methods

    Method

    Description

    setAlternate()

    See Alternating row or column colors for details.

    setBackground()

    Sets the background color for a table.

    setBottomBorder()

    Sets the bottom perimeter border of a table.

    setColumnBorder()

    Sets the vertical border used by the internal table cells.

    setHeaderBorder()

    Sets the border between the header and the first row of the table.

    setHeaderStyle()

    Sets a JCTableStyle instance to be used as a table style for the header table.

    setLeftBorder()

    Sets the left perimeter border of a table.

    setName()

    Sets the name of the table style.

    setRightBorder()

    Sets the right perimeter border of a table.

    setRowBorder()

    Sets the default border drawn between table rows.

    setTextStyle()

    Sets the default text style to be used when drawing text in table cells.

    setTopBorder()

    Sets the top perimeter border of a table.

    Alternating row or column colors

    It is often desirable to shade every other row or column differently for easier reading. The JCAlternate class is used for this purpose, and the setAlternate() method in JCTableStyle takes an instance of JCAlternate to specify the alternating colors. There are two default styles, one for columns and one for rows.

    To show rows alternating between gray and light gray, use JCAlternate.ROW. To show columns alternating between gray and light gray, use JCAlternate.COLUMN. To choose your own colors, create an instance of JCAlternate and pass its constructor the two colors you want, and a Boolean flag specifying whether the alternation is to take place over rows or over columns. If the flag is set to true, rows alternate in color, if the flag is set to false, columns alternate in color. For example, the following causes rows to alternate between red and blue:

    tableStyle.setAlternate(new JCAlternate(Color.red, Color.blue, true));

    Row/Column Dominance

    To control the order of border and cell color drawing, use the RowColumnDominance property of the table. A value of ROW_DOMINANCE forces column background colors and borders to draw before the equivalent properties of the table's row. In contrast, a value of COLUMN_DOMINANCE forces row background colors and border to draw before the equivalent properties of the table's columns. This property is set on the table class (JCPageTable or JCFlowTable) with the setRowColumnDominance() method.

    4.6.2 Adding Borders

    To define the appearance of a table border, you must select or create a JCDrawStyle with the required attributes and apply the style to the border. For information on controlling the appearance of borders, refer to Creating Draw Styles, in Chapter 6.

      table.setAllBorders(JCDrawStyle.LINE);
      table.setLeftBorder(JCDrawStyle.LINE_2POINT);
      table.setRightBorder(JCDrawStyle.LINE_2POINT);

    This example uses setAllBorders() to apply the default single line style to all borders in the table, and then uses setLeftBorder and setRightBorder to apply thick side borders. If we were to apply this style to the table we developed earlier, the result would be as follows:

    Figure 7 :  Table with borders.

    JCDrawStyle makes the following line styles available for table borders:

    LINE_TYPE_BROKEN

    Applies a dotted line to the table border.

    LINE_TYPE_DOUBLE

    Applies a double line to the table border.

    LINE_TYPE_NONE

    Blanks out the table border.

    LINE_TYPE_SINGLE

    Applies a single line to the table border.

    JCPageTable provides numerous methods for applying line styles to table borders:

    JCPageTable Method

    Result

    setAllBorders()

    Applies the style to every border in the table.

    setBottomBorder()

    Applies the style to the bottom border of the table's last row.

    setColumnBorder()

    Applies the style to all column borders, except for the perimeter borders Left and Right.

    setEdgeBorders()

    Applies the style to all borders on the perimeter of the table (Top, Bottom, Left, and Right).

    setHeaderBorder()

    Applies the style to the border between the header table and the table's first row.

    setHorizontalBorder()

    Applies the style to the Top, Bottom, and header borders.

    setInternalBorders()

    Applies the style to all row and column borders except for the perimeter borders.

    setLeftBorder()

    Applies the style to the border on the table's left-hand side.

    setRightBorder()

    Applies the style to the border on the table's right-hand side.

    setRowBorder()

    Applies the style to all table row borders except for the header border and the perimeter borders Top and Bottom.

    setTopBorder()

    Applies the style to the top border of the table's first row. If there is a header table, applies the style to the top row of the header.

    setVerticalBorders()

    Applies the style to column, Left, and Right borders.

    The behavior of perimeter borders at page breaks is determined by the BorderMode property of the table. A value of BORDER_USE_EXTERNAL forces perimeter borders to be used on each section of the multi-page table. A value of BORDER_USE_INTERNAL forces perimeter borders to be used only at the beginning and end of the table. Internal cell borders will be used for the table sections broken across pages. For example:

    Figure 8 :  External and internal border behavior.

    4.6.3 Adding Header Borders

    As mentioned in Section 4.5.3, Adding Header Rows, the header row is a separate table unto itself. Modifying header borders is just like modifying borders in the main table. For example, to apply a double line to the top and bottom of the header row, enter:

      table.setTopBorder(JCDrawStyle.LINE_DOUBLE);
      table.setHeaderBorder(JCDrawStyle.LINE_DOUBLE);

    The table appears as follows:

    Figure 9 :  Table with double header borders.

    4.6.4 Applying Background Colors

    You can further customize tables by applying background colors to cells, rows, columns, or the entire table. Colors and greyscale are defined by java.awt.Color. For more information on the colors and greys available to you, refer to the Java 1.3 API Specification at http://java.sun.com/j2se/1.3/docs/api/index.html.

    For example, suppose you wanted to apply a green background to all of the cells in the first column of the table.

      table.getColumn(0).setBackgroundColor(Color.green);

    This example uses the getColumn() method to identify the column to be colored, and then uses the setBackgroundColor() method to apply the specified static color.

    4.6.5 Adjusting the Size of a Table

    If you wish to adjust the size of a table so that it occupies all the space available in its parent frame, use the fitToFrame() method. The method takes two parameters: the frame to which the table should fit itself and the current JCTextStyle. It should only be called after all table borders, cell borders, and margins have been finalized.


    4.7 Customizing Cells

    JCPageTable has three inner classes: JCPageTable.Cell, JCPageTable.Column, and JCPageTable.Row, written to allow for more precise control over the description of individual table components.

    JCPageTable.Column and JCPageTable.Row provide methods that allow you to customize the cells in an entire column or row by modifying their alignment, background color, borders, and margins.

    JCPageTable.Cell provides attributes and methods that allow you to customize cell appearance, including settings for vertical alignment, borders, margins, and spans.

    4.7.1 Setting the Vertical Alignment

    You can use the JCPageTable.Cell.setCellAlignment() method to adjust the vertical alignment of text in a cell.

    Adding the words "Retriever" and "Scottish" to cells 0,0 and 2,0 increases the height of all of the cells in rows 0 and 2. By default, the other cells in the same row align their text to the top of the cell.

    Figure 10 :  Table cells with vertical alignment set to Top.

    Suppose, for example, you want to vertically align that text to the middle of the cell. Here is the relevant code and how the altered table will appear:

      table.setDefaultCellAlignment(JCPageTable.CELL_ALIGNMENT_CENTER);

    Figure 11 :  Table cells with vertical alignment set to Center.

    You may also control the alignment in cells individually. The first three code snippets show how to align a cell to the bottom, top, and center of a cell, respectively. The last code snippet shows how to revert to the last alignment specified.

      cell = table.getCell(0,1);
      cell.setCellAlignment(JCPageTable.CELL_ALIGNMENT_BOTTOM);

      cell = table.getCell(0,2);
      cell.setCellAlignment(JCPageTable.CELL_ALIGNMENT_TOP);

      cell = table.getCell(2,2);
      cell.setCellAlignment(JCPageTable.CELL_ALIGNMENT_CENTER);

      cell = table.getCell(2,1);
      cell.setCellAlignment(JCPageTable.CELL_ALIGNMENT_NONE );

    JCPageTable.getCell() identifies the cells containing text we want to vertically re-align. JCPageTable.Cell.setCellAlignment() adjusts their vertical alignment in the cell, in this case, to the center of the cell. Our adjustments produce the following results:

    Figure 12 :  Table cells with vertical alignment set to Center.

    JCPageTable provides the following cell vertical alignment options:

    CELL_ALIGNMENT_CENTER

    Aligns cell text to the middle of the row.

    CELL_ALIGNMENT_TOP

    Aligns cell text to the top of the row.

    CELL_ALIGNMENT_BOTTOM

    Aligns cell text to the bottom of the row.

    CELL_ALIGNMENT_NONE

    No specific cell alignment.

    4.7.2 Defining Cell Margins

    To control the space between the cell border and the text it contains, you adjust the cell margins. An enlarged version of one of the cells in the sample table provides a good example.

    Figure 13 :  Enlarged table cell.

    The program has left gaps between the text and the left, right, top, and bottom borders of the cell. You can adjust the size of these gaps using JCPageTable.Cell methods.

      cell = table.getCell(1,1);
      cell.setBottomMargin(new JCUnit.Measure(JCUnit.POINTS, 8));
      cell.setTopMargin(new JCUnit.Measure(JCUnit.POINTS, 8));
      cell.setLeftMargin(new JCUnit.Measure(JCUnit.POINTS, 2));
      cell.setRightMargin(new JCUnit.Measure(JCUnit.POINTS, 2));

    The preceding example identifies the cell (1,1 - remember that the header row is numbered separately) for which margins are to be adjusted, and then calls the appropriate JCPageTable.Cell methods to set the top and bottom margins to 8 points and the left and right margins to 2 points.

    Figure 14 :  Enlarged table cell with margins illustrated.

    4.7.3 Customizing Cell Borders

    You can customize individual cells by overriding the border style specified by the JCDrawStyle and applied by the appropriate JCPageTable method. To override the style for a particular cell's borders, identify the cell, create a new JCDrawStyle, and apply it using a method from the JCPageTable.Cell class.

      JCDrawStyle cellstyle = (JCDrawStyle)
            JCDrawStyle.stringToStyle("default line").clone();

      cellstyle.setLineType(JCDrawStyle.LINE_TYPE_DOUBLE);
      cellstyle.setLineWidth(new JCUnit.Measure(JCUnit.POINTS, 1));
      JCPageTable.Cell cell = table.getCell(1, 1);
      cell.setBottomBorderStyle(cellstyle);
      cell.setRightBorderStyle(cellstyle);

    Note that style changes can also be made in one line as follows:

      table.getCell(0, 1).setBottomBorderStyle(cellstyle);
      table.getCell(1, 0).setRightBorderStyle(cellstyle);

    After applying the above style changes to our table, this is the result:

    Figure 15 :  Table cell with customized borders.

    In order to prevent cells from overriding each other's border styles, you can only specify the appearance of a cell's right and bottom borders. To achieve the results shown in the example, the program applies the JCDrawStyle to the right and bottom borders of the cell in question (Siamese), then applies the style to the bottom border of the cell immediately above it (Persian), and to the right border of the cell to its left (Collie).

    4.7.4 Spanning Cells

    Spanning cells is the process by which the borders between specified cells are eliminated, creating one merged cell. The merged cell can cross multiple row and column borders, but does not alter the structure of the remaining cells, rows or columns in the table. Note that you should span cells before populating the cells.

    To span cells, you pass to the JCPageTable.spanCells() method the row and column number of the cell that is the upper left-hand corner of the span, along with the number of rows to span down and the number of columns to span to the right. Follow this format:

    table.spanCells(startRow, startColumn, numRows, numColumns);

    For example, to span four cells down the first column of a table, beginning with the upper left-hand corner cell in the table, enter:

      table.spanCells(0, 0, 4, 1);

    Figure 16 :  Table with spanned cells.


    4.8 Table Wrapping

    If a table is too long for the current frame, the extra rows flow to the next frame, along with the header row, which appears at the top of every frame to which the table rows flow.

    If a table is too wide for the current frame, use JCPageTable.setOverflowMode() to handle the extra column data.

    To wrap the extra column(s) so that they appear below the table:

      table.setOverflowMode(JCPageTable.OVERFLOW_WRAP_COLUMNS);

    Figure 17 :  Table with wrapped column.

    To wrap the extra column(s) onto the following page:

      table.setOverflowMode(JCPageTable.OVERFLOW_WRAP_COLUMNS_NEXT);

    Figure 18 :  Table with column wrapped to the next page.

    To clip an equal amount of data from the left and right-most columns in the table:

      table.setOverflowMode(JCPageTable.OVERFLOW_CLIP_COLUMNS);

    Figure 19 :  Table with clipped columns.


    4.9 Converting Tables

    You can use JClass PageLayout to convert data from other Java table classes into JCPageTable tables. You can extract table data along with simple text formatting from JClass JCTables and Swing JTables. You can also extract the data from a JDBC result set and reconstruct it as a JCPageTable.

    4.9.1 Converting JClass LiveTables

    If you installed JClass LiveTable with your JClass DesktopViews installation, you can flow data from a JClass LiveTable JCTable into a JCPageTable in your JClass PageLayout application.

    Creating a New JCPageTable

    To flow JCTable data into a new instance of a JCPageTable, use JCPageTableFromJTable.createTable(). You have the following options:

    Method

    Description

    createTable(JCDocument doc, JCTable
      jcTable, boolean populate)

    If populate is true, creates a JCPageTable by duplicating all of the data and text formatting stored in the JCTable. If populate is false, an empty JCPageTable is created.

    createTable(JCDocument doc,
      TableDataModeltableDataModel,
      boolean populate)

    If populate is true, creates a JCPageTable by duplicating the data stored in the data source normally used by the JCTable. Since no JCTable is passed in, no text formatting is converted. If populate is false, an empty JCPageTable is created.

    Populating an Existing JCPageTable

    To flow JCTable data into an existing JCPageTable, use JCPageTableFromJCTable.populateTable(). You have the following options:

    Method

    Description

    populateTable(JCPageTable doc, JCTable

             jcTable)

    Populates a JCPageTable with all of the data and text formatting stored in the JCTable.

    populateTable(JCPageTable doc, TableDataModel

             tableDataModel)

    Populates a JCPageTable with the data stored in the data source normally used by a JCTable. Since no JCTable is passed in, no text formatting is converted.

    4.9.2 Converting Swing JTables

    You can also convert tables created with javax.swing.JTable into JCPageTable instances.

    Creating a New JCPageTable

    To create a new JCPageTable from an existing Swing JTable or JTable TableModel, use the JCPageTableFromJTable.createTable() method. You have the following options:

    Method

    Description

    createTable(JCDocument doc,
    javax.swing.JTable jTable,
    boolean populate)

    If populate is True, creates a JCPageTable by duplicating all of the data, cell fonts, and text alignments from the view of the source JTable. If populate is False, an empty JCPageTable, with the same number of columns as the view of the source JTable, is created.

    createTable(JCDocument doc,
    javax.swing.table.TableModel tableModel,
    boolean populate)

    If populate is True, creates a JCPageTable by duplicating all of the data from the source TableModel. No text formatting is performed. If populate is False, an empty JCPageTable, with the same number of columns as the source TableModel, is created.

    Populating an Existing JCPageTable

    To flow data and text styles from an existing Swing JTable or JTable TableModel into an existing JCPageTable, use the JCPageTableFromJTable.populateTable() method. You have the following options:

    Method

    Description

    populateTable(JCPageTable table,
    javax.swing.JTable jTable,
    boolean applyJTableStyles)

     

    Populates an existing JCPageTable with all of the data from the view of the source JTable. If applyJTableStyles is true, cell fonts and text alignments from the source JTable will be copied to the JCPageTable.

    populateTable(JCPageTable table,
    javax.swing.table.TableModel tableModel)

    Populates a JCPageTable with all of the data from the source TableModel. No text formatting is performed.

    Examples

    To perform a complete conversion of an existing JTable to a JClass PageLayout PageTable:

    JCPageTable pageTable =
      JCPageTableFromJTable.create(document, jTable, true);

    To convert an existing JTable to a JClass PageLayout PageTable, without preserving text formatting from the existing JTable so that new formatting can be applied:

    // create JCPageTable with same number of columns as JTable
    JCPageTable pageTable =
      JCPageTableFromJTable.create(document, jTable, false);
    // apply our own text formatting to headers and body
    JCPageTable headerTable = pageTable.getHeaders();
    headerTable.getRow(0).setDefaultStyle(JCTextStyle.HEADING5);
    pageTable.setDefaultStyle(JCTextStyle.CODE);
    // populate JCPageTable with data from JTable
    JCPageTableFromJTable.populate(pageTable, jTable, false);

    4.9.3 Converting JDBC Databases

    JDBC (Java DataBase Connectivity) is the part of the Java API (java.sql) that allows you to send SQL queries to a database. You can format the result set of an SQL query into a JCPageTable.

    1. Create the JCPageTable and the ResultSet.   protected JCPageTable createTable(JCDocument doc) {
          ResultSet resultSet = null;
          JCPageTable table  = null;

    2. Load the JDBC driver.   try {
          Class.forName(driver); // Example: sun.jdbc.odbc.JdbcOdbcDriver
        } catch (ClassNotFoundException cnfe) {
          cnfe.printStackTrace();
        }

    3. Conduct the SQL query and return the JCPageTable.     try {
          Connection connection   = DriverManager.getConnection(url, login, password);
          Statement statement = connection.createStatement();
          resultSet = statement.executeQuery(query);
          table = com.klg.jclass.page.JCPageTableFromJDBC.createTable(doc, resultSet);

          // clean up
          resultSet.close();
          statement.close();
          connection.close();
        

        } catch (SQLException sqle) {
          JOptionPane.showMessageDialog(null, sqle.toString(), "SQL error", JOptionPane.ERROR_MESSAGE);
          return null;
        }

        // return the JCPageTable
        return table;


    PreviousNextIndex