Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with submenus using Swing

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
I have a question on creating submenus. I'm able to get the submenu name and one item added to it. Problem is, I'm reading line by line from a text file, so I'm getting mutiple submenus for the same one, each with it's own item. How can I add the items to just one submenu? It seems I may need to read them all in and hold onto them, then populate the menu.

example:
Optionmenu1 > OptionmenuItem1
Optionmenu1 > OptionmenuItem2

should show as:
Optionmenu1 > OptionmenuItem1
OptionmenuItem2


Code:
        for (int i = 0; i < myList.size(); ++i) {
            MenuConfig theMenuConfig = (MenuConfig) myList.get(i);
            System.out.println(theMenuConfig);
            JMenuItem aMenuItem = new JMenuItem(theMenuConfig.getName());
            aMenuItem.addActionListener(al);
            if (theMenuConfig.getId() == 99) { // populate Apps
                if (theMenuConfig.getLevel() == 1) { // check for submenu
                    submenuName = theMenuConfig.getName();
                    
                }else if(theMenuConfig.getLevel() == 2) { // check for submenu item
                    JMenu optionMenu = new JMenu(submenuName);
                    optionMenu.add(theMenuConfig.getName());
                    m1.add(optionMenu);
                }
                else
                    m1.add(aMenuItem);
            } else if (theMenuConfig.getId() == 1) { // populate URLs
                m2.add(aMenuItem);
            } else if (theMenuConfig.getId() == 2) { // populate Folders
                m3.add(aMenuItem);
            } else
                // populate Files
                m4.add(aMenuItem);
        }

Please see this post for entire application code:

I can post sample text file data if need be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top