by John D. Mitchell

Java Tip 12: How to make cascading menus in Java

how-to
Jul 1, 19962 mins

How to make cascading menus in Java

The plain little Java window that has popped up to annoy you exists for sole purpose of showing you how Cascading Menus work. These are also known as Pull-Right Menus since they open up to the right of their parent.

Go click on the File menu and then on its various menu items and their children to see how deep the menu nesting goes.

Here is the code for the CascadeApplet applet. Itโ€™s pretty basic in that all it does is create and then display our Frame.

Here is the code for the CascadeMenus subclass of Frame. Basically, it just creates a new Menu rather than another MenuItem whenever it wants to create a cascading menu.

Now that you know how to make cascading menus, please use them judiciously. As in my example, it is easy to make things more confusing by using Cascading menus. Also keep in mind that a number of us mouse-jockeys are spastic and therefore have a hard time navigating complicated menu hierarchies with these new-fangled mice.

Enjoy!

Subsisting on caffeine, sugar, and too little sleep, John D. Mitchell has been consulting for most of the last nine years, and developed PDA software in OO assembly language at Geoworks. He funds his Java addiction by writing compilers, Tcl/Tk, C++, and Java systems. He co-authored the hot new Java book Making Sense of Java and is currently developing a Java compiler.