public class TreeView extends Container
TreeModel
in a tabular form. TreeViews are
ubiquitous in most applications, being used to both output data in list
form, as well as allowing the user to select one or more items from a list.
TreeView is the view part of GTK's model-view-controller pattern list
Widget, with one of the TreeModel subclasses supplying the underlying data
model.
The TreeView API is very powerful, but with that power comes considerable
complexity. To build a working TreeModel backed TreeView you will need to
follow the instructions presented in the documentation quite carefully. The
remainder of this page discusses the presentation side of the API; see
DataColumn
for a detailed overview of the contents side.
A TreeView is composed of one or more vertical columns called
TreeViewColumn
s. Into these are packed
CellRenderer
s. A CellRenderer does the job of taking
data from the underlying TreeModel and rendering it in the TreeViewColumn.
There is a family of CellRenderers for different underlying data types, but
you'll use CellRendererText almost exclusively.
Let's assume we have a ListStore with a String DataColumn in it, ie
final ListStore model; final DataColumnString countryNameColumn; final TreeView view; final TreeSelection selection; TreeViewColumn vertical; CellRendererText text; ... model = new ListStore(new DataColumn[] { countryNameColumn, ... }Note that there is nothing that requires you to populate your model before building your TreeView. You can do that later - indeed, that might be the whole point of your application.
You start creating your view by instantiating a TreeView and then using it to get TreeViewColumn instances:
view = new TreeView(model); vertical = view.appendColumn(); vertical.setTitle("Country");Now you construct a CellRenderer, specifying what TreeViewColumn it's going to be a part of, and then the most important part, specifying where its data is going to come from. This is the step that binds TreeView and TreeModel.
text = new CellRendererText(vertical); text.setText(countryNameColumn);along with setting any other properties on the CellRenderer as necessary. And that's it! You will of course need to do this for each TreeViewColumn of information you wish to have showing in your TreeView. (We tend to find it easier if you reuse the TreeViewColumn and CellRenderer variable names; there is usually no real reason to keep a reference to them individually; otherwise you've got to come up with unique names for everything and that tends to make for ugly code).
Dealing with the events generated on the TreeView is either straight
forward or quite complicated, depending on what you are trying to
accomplish. If you just need a callback when the user activates a row in
the display, then the TreeView.RowActivated
signal will do the
trick fairly simply; see its documentation for an example. For anything
else, you will need to use the TreeSelection
helper
class (every TreeView automatically has one). It has a
TreeSelection.Changed
signal which you hook up to which will tell
you what row(s) are currently selected.
selection = view.getSelection();
The design of the TreeView API is such that you can have more than one view for a given TreeModel, but we tend to only create TreeModels as the place to push the text that we wish displayed, so in general you'll have one TreeModel per TreeView.
We have departed a fair way from the method call sequence used in the underlying GTK library, in particular by assuming default behaviour and combining calls where possible. This is in an effort to make the TreeView API somewhat easier to learn, more appropriate in a Java context, and easier to use for the common cases which dominate its usage.
Modifier and Type | Class and Description |
---|---|
static interface |
TreeView.RowActivated
Emitted when a row in the TreeView has been activated.
|
static interface |
TreeView.RowExpanded
Emitted when a row in the TreeView has been expanded, i.e. when its
child nodes are shown.
|
static interface |
TreeView.SelectAll
This signal is emitted when the user selects all the rows in the
TreeView.
|
Widget.ButtonPressEvent, Widget.ButtonReleaseEvent, Widget.Destroy, Widget.Draw, Widget.EnterNotifyEvent, Widget.FocusInEvent, Widget.FocusOutEvent, Widget.Hide, Widget.KeyPressEvent, Widget.KeyReleaseEvent, Widget.LeaveNotifyEvent, Widget.MapEvent, Widget.MotionNotifyEvent, Widget.PopupMenu, Widget.QueryTooltip, Widget.ScrollEvent, Widget.SizeAllocate, Widget.UnmapEvent, Widget.VisibilityNotifyEvent
Constructor and Description |
---|
TreeView()
Construct a new TreeView.
|
TreeView(TreeModel store)
Construct a new TreeView with an already established TreeModel as its
data.
|
Modifier and Type | Method and Description |
---|---|
TreeViewColumn |
appendColumn()
Create a new TreeViewColumn and add it to right-hand edge of this
TreeView.
|
void |
collapseAll()
Collapse all the (child) rows in this TreeStore backed TreeView.
|
void |
collapseRow(TreePath path)
Collapse the given row, thus hiding its children if the row was
previously expanded.
|
void |
connect(TreeView.RowActivated handler)
Hook up a
TreeView.RowActivated handler. |
void |
connect(TreeView.RowExpanded handler)
Hook up a
TreeView.RowExpanded handler. |
void |
connect(TreeView.SelectAll handler)
Hook up a
TreeView.SelectAll signal handler. |
void |
emitRowActivated(TreePath path,
TreeViewColumn vertical)
Cause a
TreeView.RowActivated signal to be emitted for the
given TreePath. |
void |
expandAll()
Expand all the rows in this TreeStore backed TreeView, making
all children visible.
|
boolean |
expandRow(TreePath path,
boolean openAll)
Expand the given row, making its children visible to the user.
|
Rectangle |
getCellArea(TreePath path,
TreeViewColumn column)
Get a Rectangle describing the area used by the CellRenderer to draw
the cell at the position described by the row
path in the
column column . |
TreeViewColumn |
getColumn(int index)
Get the TreeViewColumn at the given position in the TreeView, with
0 being the left-most one. |
TreeViewColumn |
getColumnAtPos(int x,
int y)
Figure out which TreeViewColumn a given event's co-ordinates correspond
to.
|
TreeViewColumn[] |
getColumns()
Get all the TreeViewColumns for this TreeView.
|
boolean |
getEnableSearch()
Check if the built-in quick search capability is enabled for this
TreeView.
|
boolean |
getFixedHeightMode()
Get the current fixed height mode for the TreeView.
|
Adjustment |
getHAdjustment()
Get the Adjustment currently being used for the horizontal aspect of
this TreeView.
|
TreeModel |
getModel()
Get the TreeModel currently providing the data powering this TreeView
Widget, or
null if not yet set. |
TreePath |
getPathAtPos(int x,
int y)
Get a TreePath indicating what row in the TreeView a given set of
co-ordinates corresponds to.
|
boolean |
getRubberBanding()
Get the current status of the rubber banding property of the TreeView.
|
Entry |
getSearchEntry()
Get the current Entry widget being used for the interactive search
feature for this TreeView.
|
TreeSelection |
getSelection()
Get the TreeSelection object corresponding to this TreeView.
|
TreeViewColumn |
getTooltipColumn()
Get the TreeViewColumn that is being used to retrieve text for the
toolrips for each row.
|
TreeIter |
getTreeIterFromTooltipContext(int x,
int y,
boolean keyboardMode,
TreeModel model)
Retrieve the TreeIter from the row where the mouse is currently over
for a possible tooltip or null if there isn't any row under the mouse.
|
TreePath |
getTreePathFromTooltipContext(int x,
int y,
boolean keyboardMode)
Retrieve the TreePath from the row where the mouse is currently over
for a possible tooltip or null if there isn't any row under the mouse.
|
Adjustment |
getVAdjustment()
Get the Adjustment for the vertical aspect of this TreeView.
|
boolean |
hasTooltipContext(int x,
int y,
boolean keyboardMode)
Check whether the area where the mouse is curently over is part of the
TreeView and is valid for a possible tooltip.
|
void |
removeColumn(TreeViewColumn column)
Remove a tree view column from this TreeView.
|
boolean |
rowExpanded(TreePath path)
Check whether the given row is expanded, i.e. whether its children are
shown.
|
void |
scrollToCell(TreePath path,
TreeViewColumn vertical)
Scroll the TreeView so that the cell specified by
path ,
vertical is visible. |
void |
scrollToCell(TreePath path,
TreeViewColumn vertical,
float rowAlign,
float colAlign)
Scroll the TreeView so that the cell specified by
path ,
vertical is visible. |
void |
setCursor(TreePath path,
TreeViewColumn vertical,
boolean startEditing)
Set the current keyboard focus to be at a specific path in the
TreeView.
|
void |
setEnableSearch(boolean setting)
Set whether or not the built-in quick search capability will be enabled
for this TreeView.
|
void |
setFixedHeightMode(boolean enable)
Set the fixed height mode for the TreeView.
|
void |
setHAdjustment(Adjustment adjustment)
Set the Adjustment for the horizontal aspect of this TreeView.
|
void |
setHeadersClickable(boolean setting)
Set whether the column titles in the header row can be clicked to
change the sorting of the displayed data.
|
void |
setHeadersVisible(boolean setting)
Set whether this TreeView has a header row at the top of the Widget
showing the titles of each of the TreeViewColumns packed into it.
|
void |
setModel(TreeModel store)
Set the TreeModel being used to source data for this TreeView.
|
void |
setReorderable(boolean setting)
Set whether the TreeModel being shown by this TreeView can be reordered
by dragging and dropping the rows.
|
void |
setRubberBanding(boolean enable)
Set whether rubber banding is enabled in this TreeView.
|
void |
setRulesHint(boolean setting)
Request that alternating colours be drawn in the background of the
TreeView.
|
void |
setSearchColumn(DataColumn column)
Set the column in your TreeModel which will searched through if the
user starts an interactive search.
|
void |
setSearchEntry(Entry entry)
Set an Entry to be used as an alternative to the default built-in popup
used by the the interactive search.
|
void |
setTooltipCell(Tooltip tooltip,
TreePath path,
TreeViewColumn column,
CellRenderer cell)
Set the tooltip for the current cell.
|
void |
setTooltipColumn(TreeViewColumn column)
Set a column with full pango markup text to be used for the tooltips for
each row.
|
void |
setTooltipRow(Tooltip tooltip,
TreePath path)
Set the tooltip for the entire row.
|
void |
setVAdjustment(Adjustment adjustment)
Set the Adjustment for the vertical aspect of this TreeView.
|
add, getChildren, remove, setBorderWidth
activate, addEvents, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, connect, destroy, getAllocatedHeight, getAllocatedWidth, getAllocation, getCanDefault, getCanFocus, getHasFocus, getName, getParent, getPreferredHeightForWidthMinimum, getPreferredHeightForWidthNatural, getPreferredHeightMinimum, getPreferredHeightNatural, getPreferredWidthForHeightMinimum, getPreferredWidthForHeightNatural, getPreferredWidthMinimum, getPreferredWidthNatural, getRequestMode, getRequisition, getSensitive, getStyleContext, getToplevel, getWindow, grabAdd, grabDefault, grabFocus, grabRemove, hide, isSensitive, overrideBackground, overrideColor, overrideFont, queueDraw, queueDrawArea, realize, setAlignHorizontal, setAlignVertical, setCanDefault, setCanFocus, setEvents, setExpandHorizontal, setExpandVertical, setName, setSensitive, setSizeRequest, setTooltipMarkup, setTooltipText, show, showAll
public TreeView()
setModel()
before any data will be
displayable!public TreeView(TreeModel store)
public TreeViewColumn appendColumn()
public void collapseAll()
public void collapseRow(TreePath path)
path
- The row to collapse.public void connect(TreeView.RowActivated handler)
TreeView.RowActivated
handler.public void connect(TreeView.RowExpanded handler)
TreeView.RowExpanded
handler.public void connect(TreeView.SelectAll handler)
TreeView.SelectAll
signal handler.public void emitRowActivated(TreePath path, TreeViewColumn vertical)
TreeView.RowActivated
signal to be emitted for the
given TreePath. The TreeViewColumn is optional; use null
if you don't want to specify it.public void expandAll()
public boolean expandRow(TreePath path, boolean openAll)
path
- The row to expand.openAll
- true
to recursively expand all children,
false
to expand only the given row.true
if the path refers to a valid row, and it has
child nodes. false
otherwise.public Rectangle getCellArea(TreePath path, TreeViewColumn column)
path
in the
column column
. The Rectangle will be in tree coordinates.
If path points to a row not currently being displayed, the
y
and height
attributes of the rectangle will
be 0
.
Note that taking the over a row's Rectangles widths, or over all rows' heights, would not cover the entire tree; there can be extra pixels of padding in between rows, etc.
This method is only works if the TreeView has already been realized.
public TreeViewColumn getColumn(int index)
0
being the left-most one.
If they know they are going to need it later, most people just keep a reference to the TreeViewColumn around when they create it.
public TreeViewColumn getColumnAtPos(int x, int y)
getPathAtPos()
for a detailed
discussion.
In native GTK, this is implemented as an out parameter on the same
function that powers getPathAtPos()
, but we've given it a
more coherent name here.
public TreeViewColumn[] getColumns()
public boolean getEnableSearch()
true
.
Use setEnableSearch()
to disable or
enable the search feature.
true
if the quick search capability is enabled;
false
otherwise.public boolean getFixedHeightMode()
To set the current height mode, see
setFixedHeightMode()
true
if all rows are to be of the same height;
false
otherwise.public Adjustment getHAdjustment()
null
is returned. To set this value, see
setHAdjustment()
.public TreeModel getModel()
null
if not yet set.public TreePath getPathAtPos(int x, int y)
The position indicated by (x
,y
) is in
bin co-ordinates. Usually you are working in the context of
a handler hooked up to an Event and these values should be obtained
from that Event.
See also getColumnAtPos()
for the
complementary method to find out what vertical the co-ordinates
correspond to.
It is common to create a context menu as a result of a right-click on a
TreeView. Ordinarily, you would intercept the
Widget.ButtonPressEvent
signal and then prepare your Menu
in the handler there, quite reasonably expecting that the row that you
have right-clicked on would be selected. Unfortunately, if you hook up
to that signal your code will run before the default handler and the
previously selected row will still be selected while your
handler runs. This is annoying. It is the default
Widget.ButtonPressEvent
handler which selects the new row,
so you have to manually select it yourself before acting on the
right-click. It is getPathAtPos()
which gives you the
ability to do so:
view.connect(new Widget.ButtonPressEvent() { public boolean onButtonPressEvent(Widget source, EventButton event) { final int x, y; final TreePath path; final TreeSelection selection; if (event.getButton() != MouseButton.RIGHT) { return false; } x = (int) event.getX(); y = (int) event.getY(); path = view.getPathAtPos(x, y); selection = view.getSelection(); selection.selectRow(path); // and now pop your context menu, doing // something with the row as appropriate. return true; } });
null
if the passed in co-ordinates do
not correspond to a row in the TreeView.public boolean getRubberBanding()
setRubberBanding()
for a
detailed description of how rubber banding works.public Entry getSearchEntry()
null
is returned.public TreeSelection getSelection()
public TreeViewColumn getTooltipColumn()
public TreeIter getTreeIterFromTooltipContext(int x, int y, boolean keyboardMode, TreeModel model)
onQueryTooltip()
method.public TreePath getTreePathFromTooltipContext(int x, int y, boolean keyboardMode)
QueryTooltip#onQueryTooltip
method.public Adjustment getVAdjustment()
null
.public boolean hasTooltipContext(int x, int y, boolean keyboardMode)
QueryTooltip#onQueryTooltip
method.
When this method returns false it is recommended to also return false
in the QueryTooltip handler to not show the tooltip.
When this returns true you can use either or both
getTreeIterFromTooltipContext()
and
getTreePathFromTooltipContext()
to retrieve content from your model
and bind the tooltip to either the row or the cell with either of
setTooltipRow()
and
setTooltipCell()
.public void removeColumn(TreeViewColumn column)
public boolean rowExpanded(TreePath path)
path
- The row we want to check.true
if the row is expanded, false
if
not.public void scrollToCell(TreePath path, TreeViewColumn vertical)
path
,
vertical
is visible. This variant ignores alignment values
and just scrolls the TreeView so that the cell specified is visible,
closest to whichever edge it came in from, and doing nothing if the
cell is already on screen.
See the discussion about path
or vertical
in
the other scrollToCell()
method to learn how you can scroll in a single
direction only if desired.
public void scrollToCell(TreePath path, TreeViewColumn vertical, float rowAlign, float colAlign)
path
,
vertical
is visible.
Only one of path
or vertical
need to be
specified if you only want to scroll in one dimension. If
path
is null
, then it will only scroll
horizontally; if no TreeViewColumn is specified in
vertical
(ie, likewise null
), then only
vertical scrolling will take place.
This all assumes that you've placed the TreeView is within a ScrolledWindow to enable scrolling behaviour!
rowAlign
- Determines where in the view the row specified by
path
is placed, with 0.0f
representing top, and 1.0f
representing bottom,
as usual. The constants in Alignment such as
CENTER
can be used.colAlign
- Determines where in the view the column specified by
vertical
will be placed; 0.0f
is
fully left, 1.0f
is fully right.public void setCursor(TreePath path, TreeViewColumn vertical, boolean startEditing)
path
, and as a
result is often used to draw a user's attention to a particular place.
If vertical
is supplied, the specific TreeViewColumn
indicated is selected. This is usually used in concert with setting
startEditing
to true
which causes the
TreeView to immediately start editing at the the specified row and
column (assuming, of course, that that CellRenderer has been made
mutable. See setEditable()
).
public void setEnableSearch(boolean setting)
Use setSearchColumn()
to indicate
which data source in your TreeModel is actually what the interactive
text search will seek through.
The default is true
, so this method is only called when
you wish to disable type-ahead find.
public void setFixedHeightMode(boolean enable)
To fetch the current height mode, see getFixedHeightMode()
.
enable
- true
if all rows in the TreeView are to be of
the same height; false
otherwise. The default
is false
.public void setHAdjustment(Adjustment adjustment)
getHAdjustment()
.public void setHeadersClickable(boolean setting)
false
(since you frequently have the rows ordered the way
they are for a reason and don't want to let the user be reordering the
display and getting lost in the process), calling TreeViewColumn's
setSortColumn()
will
make the headers clickable. Use this method after your column setup to
turn it off [again].public void setHeadersVisible(boolean setting)
true
, for headers to be visible.public void setModel(TreeModel store)
store
- a value of null
will remove the data model
underlying this TreeView, leaving it unset for the present.public void setReorderable(boolean setting)
The default is false
.
Incidentally, you can observe these changes by connecting to
TreeView.RowInserted
and TreeView.RowDeleted
.
public void setRubberBanding(boolean enable)
Rubber banding affects how selections work when the selection mode is
set to MULTIPLE
. When set to
true
then rubber banding will allow the user to select
multiple rows with the mouse. Rubber banding is off by default.
public void setRulesHint(boolean setting)
The default is false
, not drawing alternating row colours.
public void setSearchColumn(DataColumn column)
setSortColumn()
is
analogous).public void setSearchEntry(Entry entry)
To reset the TreeView to use the built-in popup Entry, pass in
null
.
public void setTooltipCell(Tooltip tooltip, TreePath path, TreeViewColumn column, CellRenderer cell)
public void setTooltipColumn(TreeViewColumn column)
public void setTooltipRow(Tooltip tooltip, TreePath path)
public void setVAdjustment(Adjustment adjustment)
getVAdjustment()
.