Primefaces: How to display a variable that can take only 'Y' or 'N' or a boolean value.

I need to display a field in an xhtml that can only take 'Y' or 'N'. It could be drop down list or check box, where user selects the value. The original value should be displayed as per corresponding variable value in Managed Bean. That means if value is Y or true, the listbox or checkbox should be in that state.
VakilsDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sajjadzCommented:
Hi, follow the source.
<p:selectOneMenu converter="#{genderConvertor}">
	<f:selectItem itemLabel="check_all"
		noSelectionOption="true" />
	<f:selectItems
		itemLabel="#{_val.label}"
		itemValue="#{_val}" var="_val"
		value="#{customerController.genders}" />
</p:selectOneMenu>


--------------------------------------

@ManagedBean(name = "genderConvertor")
public class GenderConvertor implements Converter {

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		if (!(value instanceof Gender)) {
			return null;
		}

		return ((Gender) value).getLabel();
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		if (value == null) {
			return null;
		}

		if (value.equals("check_all"))
			return null;

		Gender gender = Gender.genderValueOf(value);

		if (gender == null) {
			throw new ConverterException(new FacesMessage("Unknown gender: "
					+ value));
		}

		return gender;
	}
}
------------------------------


public enum Gender {
	MALE("male", "Y"), FEMALE("female", "N");
	private String label;
	private String value;

	private Gender(String label, String value) {
		this.label = label;
		this.value = value;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
	public static Gender genderValueOf(String k) {
		for (Gender c : Gender.values()) {
			if (c.getLabel().equals(k)) {
				return c;
			}
		}
		return null;
	}
}

----------------------
in customerController:

public Gender[] getGenders() {
	return Gender.values();
}

public void setGenders(Gender[] genders) {
	this.genders = genders;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VakilsDeveloperAuthor Commented:
Thanks! Works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.