Tree Selections
After you have the tree built and looking
the way you want it to, you need to start working with selections so
it does something useful. The JTree class
introduced many of the selection manipulation methods already, but
let’s take a closer look at the model for selecting paths in a
tree and the DefaultSelectionModel provided in the
javax.swing.tree package. If you’re
comfortable with selection models, you probably won’t find
anything surprising here and may want to skip to “Editing and
Rendering.”
Selections are based on rows or paths. It’s important to realize the distinction between a “row” and a “path” for trees. A path contains the list of nodes from the root of the tree to another node. Paths exist regardless of whether or not you plan to display the tree.
Rows, however, are completely dependent on the graphical display of a
tree. The easiest way to think about a row is to think of the tree as
a JList object. Each item in the list is a row on
the tree. That row corresponds to some particular path. As you expand
and collapse folders, the number of rows associated with the tree
changes. It’s the RowMapper object’s
job to relate a row number to the correct path.
Depending on your application, you may find rows or paths more efficient to deal with. If your program deals mostly with the user object data, paths will most likely be the thing to use. If you’re working with the graphical interface (automatically expanding folders and things like that) the rows may be ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access