Package com.ms.ui Previous
Previous
Contents
Contents
Index
Index
Next
Next

Class UITree

Constructors , Methods

public class UITree extends UISelector implements IUITree
{
  // Constructors
  public UITree();
  public UITree(String s);
  public UITree(Image i, String s);
  public UITree(Component comp);
  public UITree(Component comp, int indent);

  // Methods
  public Component add(String s);
  public Component add(String s, int pos);
  public Component add(Image i, String s);
  public Component add(Image i, String s, int pos);
  public Component add(Component comp);
  public Component add(Component comp, int pos);

  public void remove(Component comp);
  public void setChecked(boolean on);
  public void setLayout(LayoutManager lm);

  public Rectangle getAttachRect();
  public boolean hasChildren();

  public boolean getExpanded();
  public Component getExpander();
  public void setExpanded(boolean expanded);
  public Component setExpander(Component to);

  public int getRoleCode();
  public int getStateCode();

  public boolean action(Event e, Object o);
  public boolean keyDown(Event e, int key);

}

A class that implements a tree control. Each UITree object represents an expandable tree node. Using the add method, you can then insert child components, including other tree nodes. The following example demonstrates this process.

UITree t0, t1;

// Construct the root tree node with the text "Fruit".
t0 = new UITree("Fruit");

// Add two text child items to t0.
t0.add("Banana"); 
t0.add("Cherry"); 

// Construct a child node that has two children itself.
{
   t1 = new UITree("Apple");
   t1.add("Macintosh");
   t1.add("Empire");
}
t0.add(t1);  // Add node t1 to t0.

// Now add t0 to the container.
add(t0);

A UITree object typically uses UITreeLayout for its layout manager. By default, all UITree node children use UIExpandButton to represent the tree's expand button. (The root node does not have an expand button.) Initially, UITree nodes are not set in the expanded state. To manually expand or collapse a tree node, call its setExpanded method, as shown in the following example.

// Force t0 to display all children.
t0.setExpanded(true);

By default, a LIST_SELECT event is generated when an item in the tree node is clicked. You can trap for this event by overriding the handleEvent method, as shown in the following example.

public boolean handleEvent(Event e)
{
   if (e.id == Event.LIST_SELECT)
   {
      if (e.target == t0)
      {
         // Determine whether the "Banana" child item
         // was selected. cb1 is a UICheckButton object.
         if (t0.getSelectedItem().getName() == "Banana")
            cb1.setChecked(true);
         else
            cb1.setChecked(false);

         return (true);
      }
      else if (e.target == t1)
      {
         // Set the check box label to the item 
         // selected in t1.
         cb1.setName(t1.getSelectedItem().getName());
         return (true);
      }
   }
   return (false);
}

Note that UITree objects alone have no scrolling capabilities. The easiest way to add this support is to create a UIScrollViewer object to contain your tree control. UIScrollViewer inserts scroll bars when necessary. The following example adds a tree control to a UIScrollViewer object.

UITree myTree = new UITree();
myTree.add("first item"); // Add an item to myTree.
...  // Continue adding items to myTree.

// Now create a UIScrollViewer object to contain myTree. 
UIScrollViewer sv = new UIScrollViewer(myTree);

// Add sv to the container.
add(sv);

UITree extends UISelector.


Constructors


UITree

public UITree();

Creates a new tree node with no content.

Remarks:

Call the add method to add items to the tree node list.


UITree

public UITree(String s);

Creates a new tree node using the specified text for content.

ParameterDescription
s The text to be displayed with the node.

Remarks:

Call the add method to add items to the tree node list.


UITree

public UITree(Image i, String s);

Creates a new tree node using the specified image and text for content.

ParameterDescription
i The image to be displayed with the node.
s The text to be displayed with the node.

Remarks:

Call the add method to add items to the tree node list.


UITree

public UITree(Component comp);

Creates a new tree node using the specified component for content.

ParameterDescription
comp The component to be displayed with the node.

Remarks:

Call the add method to add items to the tree node list.


UITree

public UITree(Component comp, int indent);

Creates a new tree node with the specified component for content and the specified indent.

ParameterDescription
comp The component to be displayed with the node.
indent The indent (in pixels) to be used when laying out node children.

Remarks:

Call the add method to add items to the tree node list.


Methods


action

public boolean action(Event e, Object o);

Determines whether the tree node was double-clicked, and, if so, toggles the expanded state.

Return Value:

Returns true if the event was handled; otherwise, returns false.

ParameterDescription
e The event posted to the tree control.
o The object that posted the event.

Remarks:

The mouseDown event method (inherited through UISelector) generates an action event when the tree node is double-clicked.


add

public Component add(String s);

Adds the specified text to the end of the tree node list.

Return Value:

Returns the text component that was added.

ParameterDescription
s The text to be added.

Remarks:

The newly-added child component is shown or hidden, according to the tree node's expanded state. To remove the component, call remove.


add

public Component add(String s, int pos);

Adds the specified text to the tree node list at the specified position.

Return Value:

Returns the text component that was added.

ParameterDescription
s The text to be added.
pos The indexed position at which to add the item. The first position in the list is identified by 0; the end of the list is identified by -1.

Remarks:

The newly-added child component is shown or hidden, according to the tree node's expanded state. To remove the component, call remove.


add

public Component add(Image i, String s);

Adds the specified image and text to the end of the tree node list.

Return Value:

Returns the item component that was added.

ParameterDescription
i The image to be added.
s The text to be added.

Remarks:

The newly-added child component is shown or hidden, according to the tree node's expanded state. To remove the component, call remove.


add

public Component add(Image i, String s, int pos);

Adds the specified image and text to the tree node list at the specified position.

Return Value:

Returns the item component that was added.

ParameterDescription
i The image to be added.
s The text to be added.
pos The indexed position at which to add the item. The first position in the list is identified by 0; the end of the list is identified by -1.

Remarks:

The newly-added child component is shown or hidden, according to the tree node's expanded state. To remove the component, call remove.


add

public Component add(Component comp);

Adds the specified component to the tree node list.

Return Value:

Returns the component that was added.

ParameterDescription
comp The component to be added.

Remarks:

The newly-added child component is shown or hidden, according to the tree node's expanded state. To remove the component, call remove.


add

public Component add(Component comp, int pos);

Adds the specified component to the tree node list at the specified position.

Return Value:

Returns the component that was added.

ParameterDescription
comp The component to be added.
pos The indexed position at which to add the item. The first position in the list is identified by 0; the end of the list is identified by -1.

Remarks:

The newly-added child component is shown or hidden, according to the tree node's expanded state. To remove the component, call remove.


getAttachRect

public Rectangle getAttachRect();

Determines where the attaching line will be drawn from the tree node's parent to the tree node.

Return Value:

Returns the bounding rectangle of the content component, if the node displays a label; otherwise, returns the node's bounding rectangle.

Remarks:

This method implements getAttachRect in the IUITree interface.


getExpanded

public boolean getExpanded();

Determines whether the tree node is expanded.

Return Value:

Returns true if the node is expanded; otherwise, returns false.

Remarks:

This method implements getExpanded in the IUITree interface. To set or clear the expanded state, call setExpanded.


getExpander

public Component getExpander();

Retrieves the tree node's expand button, which indicates whether the node is expanded or collapsed.

Return Value:

Returns the component currently being used as the expand button.

Remarks:

This method implements getExpander in the IUITree interface. By default, a UITree node uses a UIExpandButton object for its expand button. The setExpander method allows you to specify the component that will be used.


getRoleCode

 public int getRoleCode();

Retrieves the role of the tree node control.

Return Value:

Returns the ROLE_SYSTEM code that best describes the role of the tree node.

Remarks:

This method returns the ROLE_SYSTEM_OUTLINE code.


getStateCode

public int getStateCode();

Retrieves the state of the tree node control.

Return Value:

Returns the combination of STATE_SYSTEM codes that best describes the state of the tree node.

Remarks:

If the tree node is expanded, the returned code contains the STATE_SYSTEM_EXPANDED flag. Otherwise, the returned code contains the STATE_SYSTEM_COLLAPSED flag.


hasChildren

public boolean hasChildren();

Determines whether the tree node has children.

Return Value:

Returns true if the node has child items; otherwise, returns false.

Remarks:

This method implements hasChildren in the IUITree interface.


keyDown

public boolean keyDown(Event e, int key);

Determines whether the RIGHT ARROW key or the LEFT ARROW key is being pressed and, if so, expands or collapses the tree node accordingly.

Return Value:

Returns true if event was handled; otherwise, returns false.

ParameterDescription
e The event posted to the tree control.
key The key that has been pressed.

Remarks:

This method is called when the tree node has focus and a key is pressed. The RIGHT ARROW key expands the node; the LEFT ARROW key collapses the node.


remove

public void remove(Component comp);

Removes the specified component from the tree node list.

Return Value:

No return value.

ParameterDescription
comp The child component to be removed.

Remarks:

To add a child component to the tree node, call the add method.


setChecked

public void setChecked(boolean on);

Sets or clears the checked state of the tree node.

Return Value:

No return value.

ParameterDescription
on If true, the checked state is set; otherwise, it is cleared.


setExpanded

public void setExpanded(boolean expanded);

Expands or collapses the tree node to show or hide the node's children.

Return Value:

No return value.

ParameterDescription
expanded If true, the node is expanded; otherwise, the node is collapsed.

Remarks:

This method implements setExpanded in the IUITree interface. To determine whether the node is currently expanded, call getExpanded.


setExpander

public Component setExpander(Component to);

Sets the tree node's expand button to the specified component. The expand button indicates whether the node is expanded or collapsed.

Return Value:

Returns the component previously used for the node's expand button.

ParameterDescription
to The component to be used for the tree node's expand button.

Remarks:

This method implements setExpander in the IUITree interface. By default, a UITree node uses a UIExpandButton object for its expand button. The getExpander method retrieves the node's current expand button.


setLayout

public void setLayout(LayoutManager lm);

Sets the layout manager for the tree control.

Return Value:

No return value.

ParameterDescription
lm The new layout manager to be used.

Remarks:

When a new UITree object is created, it uses the layout manager identified by the TREE_LAYOUT index defined in UISystem (if the node is using the default indent) or UITreeLayout otherwise.



Top© 1997 Microsoft Corporation. All rights reserved. Legal Notices.