Exercise
Today the JNDITree.java program in Listing 3.3 has shown you how to display a JNDI namespace. Today's exercise is to write a GUI version of this program using the Swing JTree class. If you already know Swing, you can use JNDITree.java as a guide for your program and go ahead and write your own JNDI browser.
If you do not know Swing, the exercise directory for Day 3 on the accompanying Web site includes a template program called JNDIBrowser.java for you to enhance. The JNDIBrowser program handles all of the Swing initialization, all you have to do is get a list of the names in the JNDI namespace and create a new javax.swing.tree.DefaultMutableTreeNode representing each name and add this to the JTree. When you add a name that is also a context, you need to add all the names in the sub-context.
Comments have been added to the JNDIBrowser.java file to show you where to add your code.
Don't worry if this sounds complexit isn't. You only have to write about 12 lines of code (most of which you can adapt from JNDITree.java).
Before you rush off and write your first piece of Java code for this book, please review the general discussion of the example code directory structure and Ant build files given on Day 2, "The J2EE Platform and Roles."
You can use the Ant build files supplied in the exercise and solution subdirectories of the Day 3 code on the Web site to compile and run your programs. Enter
asant run
to compile and run the program.
Optional ExerciseDisplay Names in Alphabetical Order
If you complete this exercise or simply run the provided solution, you will see that the JNDI names are listed in the order they were added to the context. As a second exercise, change your program to display the names in alphabetical order. The solution called JNDIBrowserSort.java program shows how this can be achieved using the java.util.TreeMap class.
You might find these programs useful for browsing the JNDI namespace while you are developing J2EE applications.