Application Techniques



Using the JTable Control

How to load a JTable control with data and customize its appearance.

About this technique

Details

Category

Java Client Techniques> Version 3 Swing-based controls

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on the Form Designer in the Tools Guide

This example displays an AgcJTable control with other controls that allow the user to customize the table's appearance. The data in the table comes from the companies table and is loaded from an AgcData control.

Importing the Swing table package   Top of page

To work with the classes for the parts of a table and its data model, include this import statement in your form:

  import javax.swing.table.*; 

Initializing form controls with table defaults   Top of page

In this example, the formActivate event initializes the controls. The code does these things:

Notes on the code

Loading the table with data   Top of page

In this example, the table displays data from the companies table. There are two columns: company names and company descriptions.

To load the data, code in the formActivate event calls the user-defined method retrieveTheData(). This method uses the loadFromRowCursor() method to populate the table from dataCompanies, an AgcData control that gets its data from the companies table.

  public void retrieveTheData() 
  { 
     try 
     { 
        // Populate the table rows 
        int[] columnIndexes =  
        {  
          dataCompanies.getPropertyIndex("companyname"), 
          dataCompanies.getPropertyIndex("description")           
        }; 
        int valueColumnIndex =     
          dataCompanies.getPropertyIndex("companyid"); 
        dataCompanies.query(""); 
         
        // Load the table from the row cursor 
        jtableCompanies.loadFromRowCursor(dataCompanies, 
              columnIndexes, null, valueColumnIndex, 
              m_sColumnTitles); 
     } 
     catch(Exception ex) 
     { 
        agDialog.displayError(ex); 
     } 
  } 

Notes on the code

The loadFromRowCursor() method uses properties of dataCompanies as the columns in the table. You specify:

loadFromRowCursor() must be called in a try/catch block.

Getting values from controls and changing the table appearance   Top of page

The controls on the form change several aspects of the table's appearance. The table lists the appearance settings and the methods that affect them.

Table setting

Corresponding set method in JTable class

Whether a click selects a cell, row, or column

  setCellSelectionEnabled(boolean) 
  setColumnSelectionAllowed (boolean) 
  setRowSelectionAllowed (boolean) 

Whether grid lines border the table cells

  setShowGrid(boolean) 

Whether the grid has horizontal lines, vertical lines, or both

  setShowHorizontalLines(boolean) 
  setShowVerticalLines(boolean) 

The height of a row

  setRowHeight(int) 

The spacing between cells

  setIntercellSpacing(Dimension) 

Whether the user can arrange columns by dragging the column headers

  setReorderingAllowed(boolean) 

(A method of the JTableHeader class)

The colors of the selection foreground (text), selection background, and grid lines

  setSelectionForeground(Color) 
  setSelectionBackground(Color) 
  setGridColor(Color) 

Changing a color   Top of page

The form presents a color selection dialog so the user can select a color visually. The current color is the default. This is the code for the actionPerformed event of the imgBackgroundColor button:

  Color colorBackground = JColorChooser.showDialog 
   ( 
     getParent(),  
     "Selection Background Color", 
     jtableCompanies.getSelectionBackground() 
   ); 
   
  jtableCompanies.setSelectionBackground (colorBackground); 

Getting a slider value   Top of page

The form uses sliders for row height and cell spacing.

This code for the valueChanged event of jSliderRow changes the row height based on the integer value of the slider:

  jtableCompanies.setRowHeight (jSliderRow.getValue()); 

This code for the valueChanged event of jSliderCell uses the slider value to construct a Dimension object for the cell spacing:

  int iSpacing = jSliderCell.getValue(); 
  jtableCompanies.setIntercellSpacing( 
     new Dimension(iSpacing, iSpacing)); 

Getting a checkbox value   Top of page

The form uses check boxes for several settings, including grid display and column reordering.

This code for the actionPerformed event of chkShowGrid turns the grid on or off according to the check box state:

  if (chkShowGrid.getState()) 
     jtableCompanies.setShowGrid(true); 
  else  
     jtableCompanies.setShowGrid(false); 

This code for the actionPerformed event of chkAllowReorder enables or disables reordering. The setReorderingAllowed() method is in the JTableHeader class:

  if (chkAllowReorder.getState()) 
     m_jTableHeader.setReorderingAllowed(true); 
  else  
     m_jTableHeader.setReorderingAllowed(false); 

Responding to the combo box   Top of page

This code gets the value the user selected:

  int iSelectedItem = cmbxSelectionChoice.getSelectedIndex(); 
  if (iSelectedItem < 0) 
     iSelectedItem = 0; 
   
  String sChoice = 
    cmbxSelectionMode.getItemAt(iSelectedItem).toString(); 

The code in the actionPerformed event for cbSelectionChoice combo box uses three JTable methods to control the selection type. For example, to enable cell selection:

  if (sChoice.equals("Cell")) 
  { 
     jtableCompanies.setCellSelectionEnabled(true); 
     jtableCompanies.setColumnSelectionAllowed (false); 
     jtableCompanies.setRowSelectionAllowed (false); 
  } 

Adding the column sorting feature   Top of page

The user can sort column rows by clicking on the column header. This feature uses two utility objects called TableMap and TableSorter. These objects were placed in a JAR (SwingUtilities.jar) and the JAR file was then added to the form. The following import statement appears in the General section of the code:

  import com.examples.utilityMethods.Swing.*; 

The following code from the formActivate event activates the sort feature by calling methods on the utility objects:

  //  Enable sorting capability for the JTable by clicking on  
  //  the header 
   TableSorter sorter = new TableSorter(jtableCompanies.getModel()); 
   jtableCompanies.setModel(sorter); 
   sorter.addMouseListenerToHeaderInTable(jtableCompanies);  

The code for TableSorter and TableMap was obtained from Sun's tutorial section for Swing Controls. For access to the commented code, go to the TableSorterDemo on Sun's website.

Allowing column reordering   Top of page

When the button labeled "allow reordering is activated, the user can rearrange the column positions by dragging the table headers. The code sets the setReorderingAllowed() method to true. This is a method called on the variable m_JTableHeader, an instance of the JTableHeader class.

  private void handle_chkAllowReorder_actionPerformed( 
    ActionEvent evt) 
    { 
      if (chkAllowReorder.getState()) 
        m_jTableHeader.setReorderingAllowed(true); 
      else  
        m_jTableHeader.setReorderingAllowed(false); 
    } 






Copyright © 2000, SilverStream Software, Inc. All rights reserved.