After the tree is built and looks 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 Section 17.7. 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. If your program deals mostly with the user object data, paths are a good choice. If you're working with the graphical interface (automatically expanding folders and the like), rows may be more useful. 17.5.1 The RowMapper InterfaceTree selections make extensive use of the RowMapper interface. (In the absence of a RowMapper, you simply cannot retrieve information on rows in the tree from classes like TreeSelectionModel. TreePath information is still available, and the tree, its model, and the selection model will all continue to work just fine.) It is a simple interface with one method:
17.5.2 The TreeSelectionModel InterfaceNow for the heart of selections. The TreeSelectionModel interface determines what a tree selection can look like. 17.5.2.1 PropertiesTreeSelectionModel contains the properties listed in Table 17-11. The selection model properties deal primarily with the current selection on the tree. The notion of a "lead" selection stems from the fact that a selection can happen as a process, not only as a single event. The lead selection is the most recently added cell in the selection. It might be the only path selected, but it might also be the most recent selection out of several in a range or discontiguous group of selections. If the selection contains more than one path, the getSelectionPath( ) method returns the first selection in the path, which may or may not be the same thing as getLeadSelectionPath( ). It's also good to remember that, if it has children, selecting a "folder" node in a tree does not imply selecting all the nodes underneath it. Having said that, the rest of the properties are fairly self-explanatory. minSelectionRow and maxSelectionRow let you get the smallest and largest selected row numbers. rowMapper holds a utility that manages the mapping between paths and row numbers. selectionPaths and selectionRows let you access the rows or paths currently selected. selectionCount tells you the number of rows selected.
17.5.2.2 ConstantsThe value of the selectionMode property must be one of the constants listed in Table 17-12 and defined in the TreeSelectionModel interface.
17.5.2.3 EventsThe TreeSelectionModel requires classes that implement the model to implement methods for registering listeners for property change events and tree selection events. TreeSelectionEvent is discussed in greater detail in Section 17.6 later in this chapter.
17.5.2.4 Selection methodsMany of these methods will look familiar if you read through the section on the JTree class. A tree passes along many of the selection calls to the selection model that supports it so that you can deal primarily with the tree itself.
17.5.3 The DefaultTreeSelectionModel ClassSwing provides a default implementation of the tree selection model that supports all three modes of selection. You can see this model in use in the example earlier in this chapter. 17.5.3.1 PropertiesThe DefaultTreeSelectionModel inherits its properties from the TreeSelectionModel interface and supplies the default values listed in Table 17-13.
17.5.3.2 EventsThe DefaultTreeSelectionModel supports the same property change and tree selection events as the TreeSelectionModel interface:
17.5.3.3 ConstantThe DefaultTreeSelectionModel contains one constant, as shown in Table 17-14.
17.5.3.4 Constructor
|