public class TreeStore extends TreeModel implements TreeDragSource, TreeDragDest, TreeSortable
ListStore is a more appropriate
choice.
Instantiating a TreeStore is done identically as with a ListStore, by
specifying an array of DataColumns. For example, given:
final TreeStore model; final DataColumnString place; ...you would build a model as follows:
model = new TreeStore(new DataColumn[] {
place = new DataColumnString(),
...
});
The caveat described in ListStore applies here; don't declare your model as
abstract type TreeModel; keep them as concrete type TreeStore so you can
get to the TreeStore specific methods for adding rows and navigating the
hierarchy.
To add a new row at the top level of the hierarchy, you just use
appendRow() as you have seen with ListStore:
parent = model.appendRow(); model.setValue(parent, place, "Europe");If, however, you want to add a new row as a child of an existing row, you need to use the
appendChild() method
instead:
child = model.appendRow(parent); model.setValue(child, place, "London"); child = model.appendRow(parent); model.setValue(child, place, "Paris"); ...passing the TreeIter representing the parent you wish to create a child under.
You may also want to read the discussion at TreePath to understand
how to address a particular row in a given TreeStore. You will also
probably want to be aware of TreeView's
expandRow(),
expandAll(), and corresponding
collapse methods.
TreeModel.RowChanged| Constructor and Description |
|---|
TreeStore(DataColumn[] types)
Construct a new TreeStore with the given column types.
|
| Modifier and Type | Method and Description |
|---|---|
TreeIter |
appendChild(TreeIter parent)
Append a new row after the last child of the given row.
|
TreeIter |
appendRow()
Append a new row at the top level of this TreeStore.
|
void |
clear()
Remove all elements from this TreeStore.
|
TreeIter |
insertRow(TreeIter parent,
int position)
Insert a new row in the TreeStore.
|
TreeIter |
insertRow(TreeIter parent,
TreeIter sibling)
Insert a new row in the TreeStore.
|
TreeIter |
iterChildren(TreeIter row)
Get the children of the given
row, if any. |
boolean |
iterHasChild(TreeIter row)
Returns whether the given
row has at least one child row. |
TreeIter |
iterParent(TreeIter row)
Get the parent of the given
row, assuming there is one. |
boolean |
removeRow(TreeIter row)
Delete a row from the TreeStore.
|
void |
setSortColumn(DataColumn column,
SortType ordering)
Specify the column from your (underlying) data model which will be used
for sorting this TreeModelSort.
|
public TreeStore(DataColumn[] types)
DataColumn for details. You must include at least
one DataColumn in your types array (you can't have a
TreeStore with no columns).public TreeIter appendChild(TreeIter parent)
setValue() methods, of course.
To add a top level row, use appendRow().
parent - The row to which a new child will be added.public TreeIter appendRow()
appendChild(parent).public void clear()
public TreeIter insertRow(TreeIter parent, int position)
parent at the position specified. If
parent is null then the new row will be
inserted at the top level of the TreeStore.public TreeIter insertRow(TreeIter parent, TreeIter sibling)
parent and will be placed in front of the supplied
sibling.public TreeIter iterChildren(TreeIter row)
row, if any.
You can use the returned TreeIter to iterate on children rows as follows:
child = model.iterChildren(row);
if (child == null) {
return;
}
do {
// do something with child row
} while (child.iterNext());
which works because iterating with a TreeIter will only iterate over
the rows that are siblings, ie, are at the same level of the hierarchy.null if the row has no children.public boolean iterHasChild(TreeIter row)
row has at least one child row.
You can use iterChildren() to get the
actual children.
true if the specified row has one or more
children, false otherwise.public TreeIter iterParent(TreeIter row)
row, assuming there is one.null if this row has no parent.public boolean removeRow(TreeIter row)
true will be returned and the
TreeIter will still be valid. Otherwise, false is returned
and row is invalidated.public void setSortColumn(DataColumn column, SortType ordering)
TreeSortableASCENDING or DESCENDING order via the
ordering parameter.setSortColumn in interface TreeSortable