Question

I have been carefully following the How to Use Trees tutorial, which explains how one can use Swing's JTree setup to display trees.

In the examples however, I have noticed that the "tree objects" are just simple strings that are displayed. Example:

enter image description here

I tried seeing if I could set up a tree that would display a small textbox (ExpressionInputBox) someone set up using [RSyntaxTextArea] and MigLayout, but I just end up getting the following happening: enter image description here

Bigger link to picture here.

What can I do to have the ExpressionInputBox show up as the nodes of the tree? Is there a limitation to what kind of objects JTree nodes can be?

Was it helpful?

Solution

Simply put, a JTree node can be objects any class as long as it implements the TreeNode or MutableTreeNode interface. That's it. You can even use classes that don't implement the interface as long as you provide a wrapper class that does.

Now you ask also:

What can I do to have the ExpressionInputBox show up as the nodes of the tree? Is there a limitation to what kind of objects JTree nodes can be?

This is a two-parter, and in the wrong order.

  • Again, for an object to be a viable node, its class must implement TreeNode.
  • To display it well (and well is the key), you will likely want to create a TreeCellRenderer that works well with it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top