0

I have seen this answer but I am unsure how to display items in p:selectOneMenu with background color backed by values from enum.

On the dropdown I would like to have items from enum, each with different color.

I am using PrimeFaces 6.2 so I am not sure how to use advice BalusC on overriding SelectOneMenuRenderer - to use custom renderer.

Here is what I've tried so far.

index.xhtml

<h:body>        
<h:form>
    <h:panelGrid id="createPanelGrid" columns="2">
        <p:outputLabel value="Color options:" />
        <p:selectOneMenu value="#{colorBean.selectedColor}" var="sc">
            <f:selectItem itemLabel="Select a color" itemValue="" />
            <f:selectItems value="#{colorBean.colorList}" 
                           var="color" 
                           itemLabel="#{color}" 
                           itemValue="#{color}" />
            <p:column>
                <div style="background-color: #{color};">
                    &nbsp;
                </div>                             
            </p:column>
            <p:column>#{sc}</p:column>
        </p:selectOneMenu>
    </h:panelGrid>
</h:form>
</h:body>

ColorBean.java

import java.io.Serializable;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named("colorBean")
@ViewScoped
public class ColorBean implements Serializable {

private String selectedColor = "White";

public ColorBean() {
}

public ColorsEnum[] getColorList() {
    return ColorsEnum.values();
}

public String getSelectedColor() {
    return selectedColor;
}

public void setSelectedColor(String selectedColor) {
    this.selectedColor = selectedColor;
}
}

ColorsEnum.java

import java.util.ArrayList;
import java.util.List;

public enum ColorsEnum {
WHITE("White"),
RED("Red"),
ORANGE("Orange"),
GREEN("Green"),
BLUE("Blue");

private final String label;

private ColorsEnum(String label) {
    this.label = label;
}

public String getLabel() {
    return this.label;
}

public static final List<String> colorList() {
    List<String> colors = new ArrayList<>();
    colors.add(ColorsEnum.WHITE.label);
    colors.add(ColorsEnum.RED.label);
    colors.add(ColorsEnum.ORANGE.label);
    colors.add(ColorsEnum.GREEN.label);
    colors.add(ColorsEnum.BLUE.label);        
    return colors;
}    
}
Syjmick
  • 51
  • 1
  • 5
  • 1
    HI, two things. What did you try? And please one question per question please. Cheers – Kukeltje Oct 20 '18 at 07:42
  • Please provide more detail in question. – Gaurav Jeswani Oct 20 '18 at 08:40
  • Edited question. What else details would you like to know? – Syjmick Oct 21 '18 at 14:12
  • Sample code in [mcve] flavour that is a serious attempt but in the end did not work. – Kukeltje Oct 21 '18 at 14:39
  • Assuming you did not only see but also try the answer you linked, using p:column in your p:selectOneMenu with var="something", you are probably biten by the fact that your enum constants get internally converted to String, and String won't be rendered with customized columns and styles by p:selectOneMenu renderer. You need to wrap your colors in some other objects and work with a converter, or extend and override a method in the selectOneMenuRenderer as BalusC suggests: https://stackoverflow.com/questions/32735770/pselectonemenu-doesnt-render-custom-content-via-pcolumn-on-liststring#32740430 – Selaron Oct 22 '18 at 09:50

0 Answers0