In this section, you will learn to display the hierarchical data in JTree . When you select the hierarchical data it is also displayed on the command prompt.
The following program constructs a tree that displays the hierarchical data. Here CellRenderer class extends JLabel and implements TreeCellRenderer. It displays the tree node like: File, Subfile and SubNode. The getTreeCellRendererComponent() method sets the value of the current tree cells. If your selection is 'true' (if any node is selected ), the cell is expanded and the selected node is displayed . The StringBuffer class implements a mutable sequence of characters. The TreeSectionListener listens to any change needed for the TreeSeclectionModel. The TreeSectionEvent handles the changes to be made in the current selection.
Finally, you get a tree that displays hierarchical data . This tree is displayed on the JFrame.
Here is the code of this program:
Output of the program:
After clicking tree node the message is displayed on the command prompt.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.