Home > Articles > Programming > Java

This chapter is from the book

This chapter is from the book

Controlling Which Nodes Can Be Edited

This simple example has a major flaw. As noted earlier, there is one editor that is shared among all of the nodes in the tree. This being the case, run the previous example again and open the tree to show the nodes for the crew of, say, Apollo 11. As you do this, be careful to click only on the expansion icons

and not on the nodes themselves or on the text, to avoid initiating the editor. Now click once on the name of any crew member and press F2. In response, you'll get the same combo box offering a choice of spacecraft! This is clearly not appropriate.

The problem here is that we have made the tree editable by calling setE-ditable(true), which allows all of its nodes to be edited. What we need is to be able to specify editability at a lower level of granularity, so that we can stop the editor appearing for specific nodes or types of node. There are two ways to do this, one of which involves subclassing JTree, the other being implemented in the tree cell editor.

  • + Share This
  • 🔖 Save To Your Account