TreeTable Nodes

Top  Previous  Next

 

Most of the TreeTableModel implementations in our framework, utilize TreeTableRow objects as the tree's nodes. TreeTableRow extends javax.swing.tree.DefaultMutableTreeNode, therefore inherits all its methods and properties. A TreeTableRow provides the values for all cells in a tree node. This can be achieved in either three ways or a combination of them:

 

1. By using the appropriate methods of TreeTableRow.

 

You can assign and retrieve the value that is displayed at the ith column with the methods:

 

public Object getAggregateValue(int columnIndex);

public void setAggregateValue(Object value, int columnIndex);

public void setAggregateValues(Object[] values);

 

2. By making use of DefaultMutableTreeNode's user object property.

 

The value at the ith column can be retrieved by setting a suitable object as the user object.

For example, consider the following Customer class:

 

public class Customer {

       String firstName;

       String lastName;

       public Customer(String firstName, String lastName) {

               this.firstName = firstName;

               this.lastName = lastName;

       }

       public String getFirstName() {

               return firstName;

       }

       public String getLastName() {

               return lastName;

       }

}

 

Assuming node is the DefaultMutableTreeNode:

 

Customer cust = new Customer("John", "Smith");

node.setUserObject(cust);

 

The cell values are then:

 

Object userObject = node.getUserObject();

Customer cust = (Customer) userObject;

String firstName = cust.getFirstName();

String lastName = cust.getLastName();

 

3. By making use of TreeTableRow's modelIndex attribute.

 

The model index can correspond to an element index in a list of objects. Once the element in the list is retrieved, we can use this object to determine the cell's value:

 

Assuming node is the TreeTableRow and objectList the list of objects:

 

int modelIndex = node.getModelIndex();

Object o = objectList.get(modelIndex);

Customer cust = (Customer) o;

String firstName = cust.getFirstName();

String lastName = cust.getLastName();