O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

Rendering Nodes

Why would I want to render a node? Good question. One reason is that you want to modify the look-and-feel 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 look-and-feel stuff for all of 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. As always, “because I can” is also a good reason.

But I only want to change the icons!

Before we tackle creating our own renderers, we should point out that the Metal look-and-feel lets you modify the set of icons used by a tree for the leaves and folders. To change the icons, you use the UIManager class and the look-and-feel icons for trees. You can also use the client property JTree.lineStyle to affect the type of lines drawn from folders to leaves. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required