Here is a picture of some tree nodes, as drawn by the Java, Windows, and Mac OS look and feel implementations. Java look and feel Windows look and feel Mac OS look and feel As the preceding figures show, a tree conventionally displays an icon and some text for each node. You can customize these, as we will show shortly. A tree typically also performs some look-and-feel-specific painting to indicate relationships between nodes. You can customize this painting in a limited way. First, you can use tree.

Author:Viran Shakree
Language:English (Spanish)
Published (Last):27 March 2008
PDF File Size:3.5 Mb
ePub File Size:18.65 Mb
Price:Free* [*Free Regsitration Required]

JTree is quite a complex component. A node is an item in a tree. A node can have many children nodes. These children nodes themselves can have further children nodes. The leaf node is displayed with a different visual indicator. Expanding a node displays the children and collapsing hides them.

Let us say we want to display the list of vegetables and fruits hierarchically. The interface MutableTreeNode extends this interface which represents a mutable node. We will be using the DefaultMutableTreeNode class to represent our node.

This class is provided in the Swing API and we can use it to represent our nodes. This class has a handy add method which takes in an instance of MutableTreeNode. So, we will first create the root node. And then we can recursively add nodes to that root. Let us start with a simple root with just 2 nodes: package net. JFrame; import javax. JTree; import javax. SwingUtilities; import javax. Note that, when we create the JTree instance, we just pass the root node instance.

This is because, the root node contains all the information including the children. Let us now run the program: Initial Output with two Children The output shows the root node and the two children. Note the difference in the icon that is displayed prior to the text. The nodes which have children are indicated with a folder icon and the leaf nodes are displayed with a different icon.

This is very simple. This indicates that these nodes have children themselves now. Also, note the handler icon the one that looks like a key that is displayed besides these nodes. These can be clicked on to expand the nodes. Let us now run the program again and click on both these handles to expand them. We get the following output: Expanded Tree Once we expand the nodes, we can see all the items in the tree displayed in a nice hierarchical structure. Also note that the handler is shown with a different indicator icon.

Let us now run the program again and click on the handles. Then, resize the frame to use less height. We will get the following output: Tree without Scrollbars As we can see, when the frame is resized, the items are hidden. Instead, we would want to display a scrollbar and allow the user to scroll up and down to see the entire tree data. Like many other Swing components, we just need to wrap our tree within a JScrollPane and add it to the frame.

Instead of adding the tree to the frame, we add the scrollpane, like: add new JScrollPane tree ; Let us now run the program again, click on the handler icons to expand the nodes and resize the frame. We will get an output like this: Tree with Scrollbars We can see that now a scrollbar is added and we can scroll to see the nodes.

Now, try and collapse one of the nodes. We can see that the scrollbar disappears. The scroll bar appears when it has items beyond the display area. The root node is the parent of all nodes, so, it would be possible for us to expand and collapse this too.

If you double-click on the root node, you can actually collapse and expand it. However, this is not very convenient and consistent with the rest of the tree. So, let us now try and display the handle for the root node. This can be done with a simple API call such as: tree. We can see that the root handle is shown: Tree with Root Handles Displayed 7. However, the display of root node may not be needed in some cases.

This is not very useful. So, what if we want to hide it? This is possible with a simple API call: tree. We get the following when the trees are expanded: Tree with Root Node Hidden Note that we still make the call to tree.

Changing the Visual Icons for JTree JTree uses different icons to represent leaf node and nodes with children as we have seen above. What if we need to provide our own icons for this purpose?

It is very much possible. We need to make use of the renderer to do this. By default, a renderer is automatically created for a JTree to display all its items. The rendering is represented by an interface called TreeCellRenderer. This class has some handy methods which we can use to customize the display.

This method takes an instance of ImageIcon class. The ImageIcon class is used to handle the display of images. Let us now run the program and expand the nodes to see the image being used for the leaf nodes. Knowing the currently selected node will be one of the most useful events to know.

Event handling in JTree is very similar to that of other Swing components. We register an event listener and are notified when the event happens. The source of the selection is passed as a parameter to this method. So, the JTree delegates all the selection related work to this class. We will first add a JLabel to be displayed at the bottom. Whenever a node in the tree is selected, we will display the path of the selected node. The TreeSelectionListener has one method which we implement. We use the event source to invoke the getPath method and set it to the label that we added earlier.

When we run the program, we get the following output: Adding a Selection Listener An even better usage of the selection listener would be to get the selected node and make use of it. This method returns the selected node. We can then invoke the getUserObject method on the DefaultMutableTreeNode class which returns the actual object we added.

Since this method returns an Object instance, we need to call the toString method on it and add it to the label. We can modify the code as follows: tree. BorderLayout; import javax. ImageIcon; import javax.

JLabel; import javax. JScrollPane; import javax. TreeSelectionEvent; import javax. TreeSelectionListener; import javax. DefaultMutableTreeNode; import javax. SOUTH ; tree.


Dynamic Tree : JTree « Swing « Java Tutorial



JTree basic tutorial and examples



How to Use Trees


Related Articles