Graphic Components > Tree Component > Tree Component Services > Handling the Selection

The selection model requirements for an IlpTree are defined by the interface IlpTreeSelectionModel. This model extends the Swing TreeSelectionModel to provide convenience operations when working with business objects (IlpObject).

The selection model is responsible for setting, modifying, and retrieving the objects selected in the IlpTree.

The IlpTreeSelectionModel interface provides the following basic methods:

This interface also has a number of advanced features described in Architecture of the Tree Component of this section.

IlpDefaultTreeSelectionModel is the default implementation of IlpTreeSelectionModel.

The following code shows you how to access the IlpTreeSelectionModel of an IlpTree.

How to Access the Selection Model of a Tree Component
IlpTree tree = ...
//...
// Retrieve the selection model
IlpTreeSelectionModel selectionModel = tree.getSelectionModel();

The following code shows you how to retrieve the selected IlpObject instances from the selection model.

How to Retrieve Selected Objects from the Selection Model
Collection selection = selectionModel.getSelectedObjects();
Iterator it = selection.iterator();
while (it.hasNext()) {
   IlpObject node = (IlpObject)it.next();
      // Do what you want with the selected node
      // ...
}

The sample <installdir>/samples/tree/basic demonstrates the use of the selection model.

Setting the Selection Look and Feel

The IlpTree allows you to choose between the look-and-feel for the selection:

images/selectionCheckBox.gif

Figure 4.2 Sample Tree using the Check Box Selection Look and feel
The following code shows you how to activate the CHECKBOX_SELECTION_LOOK_AND_FEEL.
How to Activate a Selection Look and Feel
IlpTree tree = ...
// Activate the CHECKBOX_SELECTION_LOOK_AND_FEEL
tree.setSelectionLookAndFeel
  (IlpTreeView.CHECKBOX_SELECTION_LOOK_AND_FEEL);