Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:47:16+00:00 2026-06-01T22:47:16+00:00

On my page I have a drop down whose values are populated based on

  • 0

On my page I have a drop down whose values are populated based on an enum and the selection sets the value of the enum as well . I now have requirement to display text like “Please select a Value” in the drop down and that is not present in enum.

It is also possible for user to not select any value from drop down .

How can a populate selectOne with this additional text without modifying enum and handle it in the managed bean if user does not select a value (which is legal for user to do so). I have tried various combinations but nothing worked for me .

Here is what I have working which is standard dropdown list populated with different values enum just a standar way.

This is enum here

public enum Fruit {
    APPLE("Apple"), ORANGE("Orange");

    private String label;

    Fruit(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

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

}

A Managed Bean

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

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.model.SelectItem;

@ManagedBean
public class Simplebean {

    private Fruit selectedFruit;// How would this work if the value is not valid enum .
    private List<SelectItem>fruits = new ArrayList<SelectItem>();

    @PostConstruct
    public void init(){
        for (Fruit fruit : Fruit.values()){
            fruits.add(new SelectItem(fruit,fruit.getLabel()));// How to add a value not part of enum.
        }
    }

    public List<SelectItem> getFruits() {
        return fruits;
    }

    public void setFruits(List<SelectItem> fruits) {
        this.fruits = fruits;
    }

    public Fruit getSelectedFruit() {
        return selectedFruit;
    }

    public void setSelectedFruit(Fruit selectedFruit) {
        this.selectedFruit = selectedFruit;
    }

}

In XHTML

<h:selectOneMenu value="#{simplebean.selectedFruit}">
    <f:selectItems value="#{simplebean.fruits}" />
</h:selectOneMenu>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:47:17+00:00Added an answer on June 1, 2026 at 10:47 pm

    You can do it as Luggi wrote, but add a the noSelectionOption to the additional <f:selectItem>. And a s suggestion – you don’t need to define the selectItem List List<SelectItem>fruits anymore on the backing bean, you can do it this way:

    <h:selectOneMenu value="#{simplebean.selectedFruit}">
        <f:selectItem itemLabel="Select a value" noSelectionOption="true"/>
        <f:selectItems value="#{simplebean.fruits}" var="fruit" itemLabel="#{fruit.label}" itemValue="#{fruit}"/>
    </h:selectOneMenu>
    

    And the backing bean without fruits selectItems:

    @ManagedBean
    public class Simplebean {
    
        private Fruit selectedFruit;
    
        public Fruit getSelectedFruit() {
            return selectedFruit;
        }
    
        public void setSelectedFruit(Fruit selectedFruit) {
            this.selectedFruit = selectedFruit;
        }
    
        public Fruit[] getFruits() {
            return Fruit.values();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a drop down menu on a remote page whose source i can
On my page I have a drop-down select box, with a default value of
I have a drop down menu in an ASPX page along the lines of:
I have a drop down menu that looks great when the page it scrolled
I have a drop-down list hardcoded in an MVC View User Control page and
I have a drop down list which is dynamically generated using ajax on page
I have a dynamic drop down box which calls another PHP page. I've got
I have a user control in a master page with two drop down lists.
I have one drop down which is populated from database and on the same
I've got a page which will have about ten drop down lists which are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.