dynamically creating dropDownMenu in Richfaces4
sarocks May 31, 2011 8:34 AMHi,
Creating dropDownMenu dynamically has some strange issues!!!
I have a code as below: (using Richfaces-4.0.0Final, JSF-Mojarra 2.1.0, Tomcat-6.0.20 server)
dropdownmenu.xhtml
<h:form>
<rich:toolbar height="26px" binding="#{dropDownMenuBean.menuBar}"/>
</h:form>
DropDownMenuBean.java (getter method)
public UIToolbar getMenuBar() {
FacesContext ctx = FacesContext.getCurrentInstance();
menuBar = (UIToolbar) ctx.getApplication().createComponent(UIToolbar.COMPONENT_TYPE);
UIDropDownMenu dropDownMenu = (UIDropDownMenu) ctx.getApplication().createComponent(UIDropDownMenu.COMPONENT_TYPE);
HtmlOutputText label = (HtmlOutputText) ctx.getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE);
label.setValue("File");
dropDownMenu.getFacets().put(UIDropDownMenu.Facets.label.name(), label);
dropDownMenu.setMode(Mode.ajax);
dropDownMenu.setHideDelay(0);
UIMenuItem menItm = (UIMenuItem) ctx.getApplication().createComponent(UIMenuItem.COMPONENT_TYPE);
menItm.setLabel("New");
menItm.setMode(Mode.ajax);
menItm.setIcon("/images/icons/create_doc.gif");
Class[] params = {};
MethodExpression me = ctx.getApplication().getExpressionFactory().createMethodExpression(ctx.getELContext(),
"#{dropDownMenuBean.doNew}", String.class, params);
menItm.setActionExpression(me);
dropDownMenu.getChildren().add(menItm);
menuBar.getChildren().add(dropDownMenu);
return menuBar;
}
With this much code if I run the page, only text displayed in the page as below:
But when I modify the xhtml page as below:
<h:form>
<rich:toolbar height="26px" binding="#{dropDownMenuBean.menuBar}"/>
<rich:toolbar height="26px" rendered="false">
<rich:dropDownMenu mode="ajax"/>
</rich:toolbar>
</h:form>
Now page looks better, When I move mouse cursor over "File", its loading the dropdownmenu.
But anyway clicking on "new", doesn't do any action!
So I am little confused with the concept here. I need help on this immediately.
How can I create menus dynamically and with ajax feature?
Thanks in advance,
Saroj