JTree Select

Example of using JTree to select and display information

This tutorial runs through how to change information displayed based on selecting a JTree node.  As each tree node is selected the information displayed on the right changes.

 

Download source code and art work
(you will need to provide an email address)


In a previous tutorial we looked at displaying information in a hierarchy using JTree.  Now all this is fine but, usually when we use a JTree it is because we want to be able to select an item, one or more. To do this we need to use the event driven notification for the JTree. There are different events for different situations; in our case we are interested in the selection event. When one of the options in the JTree is selected then we want to know about this.

Now the JTree comes with the ability to notify other objects when a node is selected, so the good news is we can just make use of that. What we need to do is have an object that will take on responsibility for listening to events from the JTree and do something with these events. Any object can fulfil this roll all we need to do is change the class to say that it implements the TreeSelectionListener interface which is the interface required for JTree to be able to notify of selection changes. Once we have stated the class implements the interface then we need to implement the methods for that interface and in this case there is a single method call valueChanged(TreeSelectionEvent). Within this method we can process the event information and take some action.

Well that’s the general approach now down to the implementation. We will now change our application so that it makes use of a split pane and have on one side the JTree and on the other the information associated with the selected node that we want to display.

First off we build a simple class that creates a JPanel and has a single JLabel field which we will use to display our information. We will also make this class implement the TreeSelectionListener interface so that it is capable of receiving notification of any tree selection events.

The valueChanged(TreeSelectionEvent) method checks to see if a node has been selected and if so obtains the object and uses the toString() method to set the text on the JLabel for the display panel.

So in order for our new PizzaPanel class to receive events from our JTree we need to let JTree know we are interested in these events. As we have explained JTree already has a means of notifying objects that are interested so really all we need to do is find a way of telling JTree this object is interested.

For this example we will build a new main class which still has all the same elements from the previous example, albeit moved around somewhat. Our class still extends JFrame but now we use the default constructor to set the on close action, title and layout. We also call a new private method to get the split pane and add it to the frame.

The getSplitPane() method gets the containers for the left and right sides of the split pane, once again calling private methods to do this. The term container is used here because the objects returned contain methods which return the JPanel but are not themselves JPanel objects. Once we have a handle to these containers we would then make a call to get the JTree from the JTreePanelThree object, except the previous version does not support this, so that’s a new method we need to add into that class. We need to get access to the JTree because in order to add our PizzaPanel as a listener for the event we need to call the JTree.addTreeSelectionListener() method.

Once we have added the listener we can then call the getJpanel() method for both containers adding the returned JPanels to the left and right sides of the split pane. Our new main method now creates an instance of the class and displays the frame.

As we can see from this new application, as we select nodes in the JTree the text of the node is displayed in the right hand side panel.

You can download the source code from https://softwarepulse.co.uk , all we ask is for your email address to send to a link to the download page and to keep you updated on what’s new. You can unsubscribe at any time and we do not share your details with anyone else.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.