public class TreeSelection extends Object
TreeSelection can be used to programmatically select rows by either
TreeIter or TreePath; see the selectRow()
methods. For
example, to select the first row you could do:
final TreeSelection selection; selection = view.getSelection(); selection.selectRow(new TreePath("0"));
Using TreeSelection to determine the currently selected row is fairly straight forward:
selection.connect(new TreeSelection.Changed() { public void onChanged(TreeSelection source) { final TreeIter row; row = selection.getSelected(); if (row != null) { // do something! } } });Unfortunately, the
TreeSelection.Changed
signal is not
entirely deterministic; it is sometimes emitted more than once or for no
change at all. You'll need to allow for this in your code.
Mostly this is an API helper; the underlying documentation notes that
these could have all been methods on GtkTreeView
.
Modifier and Type | Class and Description |
---|---|
static interface |
TreeSelection.Changed
Emitted when the selection state of the TreeView changes.
|
Modifier and Type | Method and Description |
---|---|
void |
connect(TreeSelection.Changed handler)
Hook up a
TreeSelection.Changed signal handler. |
SelectionMode |
getMode()
Get the mode that is governing selection behaviour on this TreeView.
|
TreeIter |
getSelected()
Get the selected row from the TreeView.
|
TreePath[] |
getSelectedRows()
Get the rows currently selected from the TreeView.
|
boolean |
isSelected(TreePath path)
Return
true if the currently selected row is pointed by
path . |
void |
selectRow(TreeIter row)
Select a row in the TreeView.
|
void |
selectRow(TreePath path)
Select a row in the TreeView.
|
void |
setMode(SelectionMode type)
Set what kinds of selections are allowed.
|
void |
unselectAll()
Unselect all the rows in the TreeView.
|
public void connect(TreeSelection.Changed handler)
TreeSelection.Changed
signal handler.public SelectionMode getMode()
public TreeIter getSelected()
SINGLE
or
BROWSE
.null
if there is no currently selected row.public TreePath[] getSelectedRows()
MULTIPLE
. Otherwise getSelected()
offers a more
convenient way to obtain the selected row.
You can use the TreeModel's getIter()
method to convert the returned TreePaths to the more
convenient TreeIter:
TreePath[] rows; TreeSelection selection; TreeModel model; rows = selection.getSelectedRows(); for (int i = 0; i < rows.length; ++i) { TreeIter row = model.getIter(rows[i]); // do something with the row }
Also remember that both TreeIter and TreePath are temporally objects no
longer valid once you make any change to the model. Thus, if you plan
to modify the model, you may want to convert the returned TreePaths to
TreeRowReferences
.
public boolean isSelected(TreePath path)
true
if the currently selected row is pointed by
path
.public void selectRow(TreeIter row)
selectRow(TreePath)
.public void selectRow(TreePath path)
selectRow(TreeIter)
for the other. Use this one if you have
want to express expressing the row you want selected in abstract,
logical form.public void setMode(SelectionMode type)
public void unselectAll()