1

I have to display a list box with label as value "name" & I am using h:selectOneListbox.

My Code is :

<h:selectOneListbox id="select" value"#{trial.trials}" size="1" title="Select Item...">
<f:selectItems value="#{trial.trials}/>
</h:selectOneListbox>

My trial bean is :

public class trial{

List<trialDataBean> trials = new ArrayList<trialDataBean>();


public trial(){
trialDatBean tdb = new trialDataBean(1,"aatmiya");
trials.add(tdb);
}

public List<trialDataBean> getTrials(){
return trials;
}

public void setTrials() {
this.trials = trials;
}

}

trialDataBean has a property "name" & I want to set it as a label of the ListBox. How do I do this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

3

In JSF 1.x, you need to create a List<SelectItem> based on your List<Trial>. The constructor of SelectItem can take the option value as 1st argument and the option label as 2nd argument.

public class Bean {

    private Trial selectedTrial;
    private List<Trial> trials;
    private List<SelectItem> selectTrials;

    public Bean() {
        trials = loadItSomehow();
        selectTrials = new ArrayList<SelectItem>();
        for (Trial trial : trials) {
            selectTrials.add(new SelectItem(trial, trial.getName()));
        }
    }

    // ...
}

Then you can use it in the view as follows:

<h:selectOneListbox value="#{bean.selectedTrial}" converter="trialConverter">
    <f:selectItems value="#{bean.selectTrials}" />
</h:selectOneListbox>

You only need to supply a custom Converter which converts between Trial and String. More detail can be found in this answer.


In JSF 2.x, you can omit the List<SelectItem> and use the new var attribute in f:selectItems instead:

<h:selectOneListbox value="#{bean.selectedTrial}" converter="trialConverter">
    <f:selectItems value="#{bean.trials}" var="trial"
        itemValue="#{trial}" itemLabel="#{trial.name}" />
</h:selectOneListbox>

 
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Just switched from JSF1 to JSF2 and loving the new var attribute on h:selectOneListbox. – scottyab Feb 11 '11 at 11:10
  • any idea if and how I can handle the double click on an item? – Alina Danila Apr 26 '12 at 20:33
  • I am following what you (BalusC) have done for JSF 2 but when I inspect the element in chrome each of my options has the whole List as its value. Here is my code: Java private ConnectedUser connection_to_remove; private List connected_users; On form submit I want to remove the `connection_to_remove` from the `connected_users` List. JSP: – edhedges Jun 12 '12 at 15:38
  • 1
    @edhedges: then you're not actually using JSF 2.x, or you're running JSF 2.x in JSF 1.x fallback modus due to bad configuration. – BalusC Jun 12 '12 at 15:43
0

You can use like this. I am not sure it will work or not because I have used <ice:selectOneMenu> tag and it worked perfectly.

<ice:selectOneListbox 
    id="paymnent" rows="10" tabindex="4"
    value="#{paymentVoucherReportAction.reportType}"
    style="width: 200px;height: 20px;">
    <f:selectItems id="AutoCmpTasdfasdfasdxtItms11"
        value="#{paymentVoucherReportAction.lstKeyValueData}" />
</ice:selectOneListbox>

// Bean(Action) File

    private List<SelectItem> lstKeyValueData = new ArrayList<SelectItem>(); // getter + setter

    private String reportType;  // getter + setter

    // put this in your init method

    List< SelectItem> list = new ArrayList< SelectItem>();

    list.add(new SelectItem("PDF Format","PDF Format"));

    list.add(new SelectItem("XLS Format","XLS Format"));

    setLstKeyValueData(list);

  // print this where you want

   System.out.println(reportType);
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Jimit Tank
  • 1,479
  • 5
  • 19
  • 25