Last updated April 18, 2012 17:59, by sonyabarry
Feedicon  

Dynamic menu builder Wiki


This project is about creation of a dynamic menu using configuration data. If this is an application menu, then you may want to even launch these applications using menus. This is a very simple requirement to design and implement a solution. It is a nice idea to make this a generic solution not associated with any specific application so that you can integrate dynamic menu builder with any application. This solution is implemented using Swing framework and Java.

Figure 1 screen shot shows dynamic menus built using a XML configuration file. You can see that some of these items are enabled and some are disabled. This data is also from the XML configuration file. Another item to note here is that these menu items launch various screens. These are nothing but internal frames added to a desktop pane. Application reads this data from a configuration file, parse the data and launch the screens.

Creating application configuration file

This is a very simple configuration file for menu configuration. Some of the attributes used for menu configuration data are caption, id and enabled option.

Figure 2 application configuration file

Figure 3 Sequence diagram for reading application data from configuration file

Figure 3 sequence diagram shows sequence diagram for loading menu data and parsing application data using SAX parser. Figure 4 shows code for creating applications and this code is self-explanatory and it reads application configuration file and invokes SAX parser parse method.

Figure 4 code to create applications

Parsing Menu configuration data

Figure 5 startelement code

  

As shown in Figure 5, startelement method reads configuration and adds item to the stack. If stack item equals application, then menu bar is added to stack. In this code, you see that swing menu and menu items are added and removed from the stack. Stack is used here is push these elements when start element is encountered and pop elements when end method is invoked. By using stack here, it is now an easy task to add menu items to menu and menus to menu bar.

Figure 6 endelement method

Sequence diagram for launching frames is given in Figure 7. User selects menu item from the list of items in main frame. Menu class action is added as action for the menu items. Object instance is created in action performed method. Object instance is created from the class information using reflection utilities. This object instance is nothing but internal frame for menu item.

Figure 7 Launch frames

Sample code for launching frames using reflection utilities is given in Figure 8.This sample code creates abstract frame using reflection and adds frame to desktop pane.

Figure 8 code to launch frames

About the author

Krishna has more than eight years experience in IT industry and Java/ J2EE technologies. Krishna's mail id is msvbkrishna@yahoo.com

References

http://java.sun.com - swing framework










  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close