Adding a table or tree view

A table or tree provides a view of objects from a data model that organizes objects in a tabular or expandable tree format. VisualAge provides table and tree beans from the com.sun.java.swing and com.sun.java.swing.table packages.


Bean Description
JTable (Swing) A table view of objects from a table data model
JTree (Swing) A tree view of objects from a tree data model

Bean--specific tasks

For examples, see the DirectoryExplorer class in the com.ibm.ivj.examples.vc.swing.directoryexplorer package and the Amortization class in the com.ibm.ivj.examples.vc.swing.mortgageamortizer package. These examples are shipped in the IBM Java Examples project.


Related procedures
Visual bean basics
Using VisualAge beans in visual composition
Adding the IBM Java Examples project

Related references
Table and tree beans
MortgageAmortizor sample

JTable and Table tasks

Defining a table data model

Create a data model class as a subclass of the AbstractTableModel class. The AbstractTableModel provides most of the TableModel interface, but you need to implement the following methods:


getRowCount() This method returns 0 by default. Return the number of rows in the data array.
getColumnCount() This method returns 0 by default. Return the number of columns in the data array.
getValueAt(int, int) This method returns null by default. Return the data array object at the row and column specified by the arguments.

You must provide column names and row data. In the Methods page, create a field of column names as an array of Strings with an initial value of names. Populate the data model with data that is either fixed or dynamically derived. You can create a field for row data as a two-dimensional array of Objects, or as a Vector.

To use a data model class as the data model for a JTable in the Visual Composition Editor, do the following:

  1. Add the class to the free-form surface.
  2. Connect the data model's this property to the table's model property

Defining table columns

By default, a table uses all columns for each row in the data model. To display data in a subset of columns or reorder the columns,

  1. For each column you want to use, add a TableColumn bean.
  2. Map the table column to the data model column from the TableColumn's property sheet, by specifying the 0-based index of the model column for the modelIndex property.
  3. Customize the column heading by specifying a value for the headerValue property.

Getting a selection from a table


To get the... Make a connection from the table's...
Selected row selectedRow property
Selected column selectedColumn property
Contents of a selected cell
  1. getValueAt() method to the target property or parameter.
  2. Connect the selectedRow property to the first parameter of the getValueAt() connection.
  3. Connect the selectedColumn property to the second parameter.

JTree and Tree tasks

Defining a tree data model

Create a data model class as a subclass of the DefaultTreeModel class. Define the tree nodes in the data model class.

To use a data model class as the data model for a JTree in the Visual Composition Editor, do the following:

  1. Add the class to the free-form surface.
  2. Connect the data model's this property to the tree's model property.