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.
Visual bean basics
Using VisualAge beans in visual composition
Adding the IBM Java Examples project
Table and tree beans
MortgageAmortizor sample
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:
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,
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 |
|
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: