As suggested by @Jasper, I set the styleClass
attribute for the submenu and it worked. Here is the code for my xhtml:
<div class="">
<h:form>
<p:panelMenu model= "#{menuBean.getModel()}" type="tiered">
</p:panelMenu>
<p:spacer height="10"></p:spacer>
</h:form>
</div>
CSS:
.icon-1 > h3 > span{
background-image: url(../resources/images/icon_1.png);
margin: 0px 4px 0px 0px;
width: 25px;
height: 25px;
}
.icon-2 > h3 > span {
background-image: url(../resources/images/icon_2.png);
margin: 0px 4px 0px 0px;
width: 25px;
height: 25px;
}
Java:
List<Items> list = //map of items to be added to the panelMenu with a parent-child hierarchy.
String label = "SubMenu 1";
String icon = "icon-1";
Creating a new submenu and adding styleClass
:
Submenu submenu = new Submenu();
submenu.setLabel(label);
//submenu.setIcon(icon);
submenu.setStyleClass(icon);
Adding the submenu to the panelMenu:
((MenuModel) container).addSubmenu(submenu);
Creating a new item to go inside the submenu:
MenuItem item = new MenuItem();
item.setValue(label);
item.setTitle(label);
item.setIcon(icon);
if (container instanceof MenuModel) {
((MenuModel) container).addMenuItem(item);
} else if (container instanceof Submenu) {
((Submenu) container).getChildren().add(item);
}
The result:

This method works much better than the first method I tried. For that, I added a custom PanelMenu renderer as explained here: Are user icons supported on root submenus in PrimeFaces 6.0 PanelMenu. It is a lot more work while requiring the same icon styling classes.