Rendering and Editing
As with the table cells covered in previous chapters, you
can create your own tree cell renderers and editors. The default
renderers and editors usually do the trick, but you’re probably reading
this because they don’t do the trick for you, so forge onward! If you
went through building your own renderers and editors for tables, you’ll
find this material quite familiar. The tree uses renderers and editors
in much the same way that tables do. In fact, you might recall that the
DefaultCellEditor
class can return both table and tree cell editors.
Rendering Nodes
Why would you want to render a node? Good question. One reason is that you want to modify the L&F of a tree without writing a whole UI package for trees. If you had some special way of presenting the “selected” look, for example, you could write your own tree renderer and still use the default L&F for your other components. You might want to render something other than a string with an icon for the nodes of your tree. Or, as we mentioned above, you might want tooltips that vary based on the particular node you rest your cursor on. “Because I can” is also a good reason.
But I just want to change the icons!
Before we tackle creating our own renderers, we should
point out that the Metal L&F lets you modify the set of icons used by
a tree for the leaves and folders. To change the icons, use the
UIManager
class and the L&F
icons for trees. You can also use the client property JTree.lineStyle
to affect the type ...
Get Java Swing, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.