Dynamic Menus

E

Eric Bresie

I'm sure this a newbie mistake when dealing with menu items and their
existance, or unfamiliarity with assorted collection classes but here
it goes..

I am trying to develop an application which gets an array of items.
With this array, I want to creates a dynamic menu. Each of the items
contains a category which represents the menu they will be a part of
and the item itself.

I was going to try one of few ways..

(1) I plan to parse through the array once to identfy all the
categories avoiding duplicates, then go back through again an add the
menuitem to the category menu. I was considering using a SortedSet
(TreeSet) to help avoid duplicate categories, but in I'm not sure if
this is the best way of doing so.

How do you look for a specific menu item?

It would look something like this:

Row[] row = Source.getRows();
SortedSet set = new TreeSet();

// Find all categories avoiding duplicates via set class
for (int i=0; i< row.length; i++ ) {
set.add( row.getCategory() ) ;
}

// Get array of category strings
String [] cats = (String [])set.toArray();

// Create all categories
for (int i=0; i< set.length; i++ ) {
catMenu = new MenuItem( cat );
menuBar.add( catMenu ) ;
}

// Add each menu item to its existing category
for (int i=0; i< row.length; i++ ) {
cat = row.getCategory();
menuName = row.getName();
Menu menu = new Menu( menuName );
whichMenu = getMenuIndex( menuName );
catMenu[whichMenu].add( menu );
}

int getMenuIndex(String name ) {
// What goes here?
}

My gut tells me this is so the wrong way to do things...

(2) My simpler way (and probably the better way; anyone remember
K.I.S.S.) of doing so is to read each item, check if the category menu
exists, if not, create the menu. Once the menu exists, add the
menuitem to the menu. How is the best way to check if a given menu
exists?

It would look something like this:

Row[] row = Source.getRows();

for (int i=0; i< row.length; i++ ) {
String cat = row.getCategory();
String menu = row.getName();

if ( ! menuExist( cat ) ) {
catMenu = new Menu( cat );
menuBar.add( catMenu ) ;
}
catMenu.add ( new MenuItem( menu ) );
}

boolean menuExist( String item ) {
// What goes here?
}

(3) Is the a Hash or Map of some type that might make this simpler?

How would be the best way to approach this?

Eric Bresie
(e-mail address removed)
 
T

Thomas Weidenfeller

Eric said:
(3) Is the a Hash or Map of some type that might make this simpler?

I would start with a map for convenience. Something like:

HashMap menus = new HashMap();

Menu menuForCategory(String category) {
Menu result = (Menu) menus.get(category);
if(result == null) {
result = new Menu(category);
menus.add(result);
}
return result;
}


/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top