menubar items

Hi:

I do I get an item from a menu bar?..I want to add a style when an item is clicked, which I get using the command:
selectedItem.setStyleName(“menu”);

This works fine, but as I add the new style to the clicked item, I want to remove that same style from any previous item that has it, but I only have the
getItems()
method, which gives me an array of menuitems, and then when I iterate over them I can’t select the item to remove the style…

Here’s is what I do to remove the style and then add the “selected” style, but it only adds the style, not remove it from the others:

Iterator itr = menu.getItems().iterator();
 while(itr.hasNext()){
	((MenuItem) itr.next()).setStyleName("v-menubar-menuitem"); 
}
selectedItem.setStyleName("menuSelected");

any ideas?..

regards,

Hugo

Hi!!!..any ideas on this???

Your code looks correctly to me, setStylename() should override all previously defined stylenames. But there might be exceptions for “predefined” styles as used here (“v-menubar-menuitem”). Not sure what setStylename() does with those. I would try to just call
removeStylename(“selected”)
on all menu-items (hoping that it does nothing if there is no such style added) and then add the style again to the now selected item.

Never worked with menubars, but I wonder if this shouldn’t be actually a builtin feature.

Just checked it in the Sampler and indeed: selected menuitems get a “selected”-style out of the box. It is named v-menubar-menuitem-selected. So the stuff you are trying to do manually should just work, as long as not totally misinterpreted your question.

Thank you for your answer Tobias…

…apparently is not working for me, because when I try to use the removeStyleName method I have to cast the iterator to a Component or Abstract Component, and later I get the message that I can’t cast MenuItems to components…

regarding you other answer, the selected item style applies to sub menu items, not menu items, like the available property checkable, wich also applies to sub menu items only…

regards,

Hugo

Oh, yes, indeed, MenuItem is not a Component. But also the top-level Items get a selected style: v-menubar-menuitem v-menubar-menuitem-selected

Thanks Tobias…

But if I remove or add any style to the menu bar ( without the iteration) it doesn’t remove the style from the items…I don’t understand why I can use setStyleName with items and not removeStyleNames…

That is because the class MenuItem has a method setStyleName() but no removeStayleName(). It is not a Component and therefore doesn’t inherit those methods. You could try to mimick removeStyleName() by using setStyleName(“”);.