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. You should not use these beans with AWT components. Although Swing and AWT beans can be mixed, it is inadvisable.

  1. Add one of the following beans:


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

  2. Define initial characteristics of the component.

    Defining tool tip text
    For Swing components, you can specify tool tip text, also known as fly-over text or hover help. Enter text for the toolTipText property in the component's property sheet. To provide tool tip text for table cells, set the toolTipText property of the cell renderer.

    Defining initial availability
    By default, the component is initially enabled for user interaction. To initially disable the component, set the enabled property to False in the component's property sheet.

  3. Add a data model for the component.

    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 will need to implement the following methods:
    getRowCount() By default, this method returns 0. You should return the number of rows in the data array.
    getColumnCount() By default, this method returns 0. You should return the number of columns in the data array.
    getValueAt(int, int) By default, this method returns null. You should return the data array object at the row and column specified by the arguments.

    You will also need to provide column names and row data. You can do this in the Methods page by by creating a field of column names as an array of Strings whose initial value is names. You can populate the data model with data that is either fixed or dynamically derived. You could create a field for row data as a two-dimensional array of Objects, or as a Vector.

    To use a data model class in the Visual Composition Editor as the data model for a JTable, add the class to the free-form surface. Then, 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. If you want to display data in a subset of columns or reorder the columns, add a TableColumn bean to the table for each column you want to use. Map the table column to the data model column by specifying the 0-based index of the model column for the modelIndex property in the TableColumn's property sheet. Customize the column heading by specifying a value for the headerValue property.

    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 in the Visual Composition Editor as the data model for a JTrele, add the class to the free-form surface. Then, connect the data model's this property to the tree's model property.

  4. Provide runtime logic for the component.

    Getting a selection from a table

    • To get the selected row, make a connection from the table's selectedRow property.

    • To get the selected column, make a connection from the table's selectedColumn property.

    • To get the contents of a selected cell, connect the table's getValueAt() method to the target property or parameter. Then, connect the selectedRow property to the first parameter of the getValueAt() connection, and the selectedColumn property to the second parameter.

    Disabling or enabling a component
    Connect a related event to the button's enabled property. Specify the parameter value for this connection in one of the following ways:

    • To disable the component, open the connection's properties and set the parameter value to False.

    • To enable the component, open the connection's properties and set the parameter value to True.

    • To set the new state from another Boolean property, connect the parameter to the other Boolean property.

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
Working with Beans Visually
Using VisualAge Beans in Visual Composition
Adding the IBM Java Examples project

Related references
Table and Tree Beans
MortgageAmortizor Sample