1

I'm having trouble with my h:selectOneMenu and f:selectItems

I want the "value" to be taken from my array of numbers called list

private double list = {51.1511, 53.51351, 0.634343, 2.52555}

and the labels for each of these values to be

private String curr = {PYN, DKT, ALT, BIT}

Currently i do

<h:selectOneMenu value="#{serviceBean.select2}">
    <f:selectItems value="#{serviceBean.list}"  itemLabel="#{serviceBean.curr}" />
</h:selectOneMenu>

This works giving me the value as the numbers but instead of showing me the strings inside the item label it shows memory references instead. How do i get this to display the strings?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Kilzrus
  • 143
  • 1
  • 11
  • Tye this : private final List curr = Arrays.asList( "PYN", "DKT", "ALT", "BIT" ); – The Bitman Mar 26 '17 at 18:40
  • It doesn't let me create a setter/getter for that so now my selectOneMenu cannot see it in the xhtml file? edit: i just created my own getter using curr.toString() and now it displays each select item as a full array so every line now looks like this [PYN, DKT, ALT, BIT] [PYN, DKT, ALT, BIT] – Kilzrus Mar 26 '17 at 18:46

2 Answers2

1
public class General{
   private double listValue;
   private String curValue;

   public General(String curValue, double listValue){
     this.listValue = listValue;
     this.curValue = curValue;
   } 

   public double getListValue(){ return listValue;}
   public String getCurValue(){ return curValue;}
}

I assume that you generated your objects and create an arraylist ArrayList<General> myvalues

ArrayList<General> myvalues = new ArrayList<>();
myvlues.add(new General("PYN",51.112));
...

<h:selectOneMenu value="#{serviceBean.selectedItem}">
<f:selectItems value="#{serviceBean.myvalues}"
                 var="myvalue"
                 itemValue="#{myvalue.listValue}"
                 itemLabel="#{myvalue.curValue}"/>
</h:selectOneMen>
misman
  • 1,347
  • 3
  • 21
  • 39
  • Hi, i have tried this but still doesn't work this just gives me the error Cant instantiate class: Bean.ServiceBean. I don't understand why value="" allows me to read the array but both itemValue="" and itemLabel="" print out the memory location. What is causing this and is there a way for me to bypass it using some type of .toString()? – Kilzrus Mar 26 '17 at 20:32
  • 1
    Because the value can be an object and value and label can be different. so just passing object and its toString method not enough in most cases. Anyway now change and check `` and if this is not the main error please write the whole error line – misman Mar 26 '17 at 20:43
  • Sorry what do you mean by change value="#{serviceBean.select2}" ? Change it to what? i have select1 and select2 as variables so that i can pass the outcome into parameters of my webservice. i still get "Can't instantiate class: Bean.ServiceBean" – Kilzrus Mar 26 '17 at 21:16
  • Sorry about that, I have fixed it for some reason my Curr class was interfering with the bean... Fixed it now thanks a lot your solution worked :) I created a object with the two attributes name and rate then created an arraylist to store it in and now the values work using var="" itemValue="" itemName="" – Kilzrus Mar 26 '17 at 21:19
  • You are Welcome and dont forget to accept answer :) – misman Mar 26 '17 at 21:37
0

You should use managed beans to access model contains in the presentation tier.

The managed bean:

@Named
@SessionScoped
public class MyBean
{
  private final List<Double> values = Arrays.asList( 1.1, 2.1, 3.1 );
  private final List<String> labels = Arrays.asList( "AAA", "BBB", "CCC" );
  private Double selectedItem;

  public List<String> getItems()
  {
    return labels;
  }

  public Double getValueAt( int ndx_ )
  { 
    return values.get( ndx_ );
  }

  public String getLabelAt( int ndx_ )
  {
    return labels.get(  ndx_ );
  }

  public Double getSelectedItem()
  {
    return selectedItem;
  }

  public void setSelectedItem( Double selectedItem_ )
  {
    selectedItem = selectedItem_;
  }

}

The JSF facelet:

<h:selectOneMenu value="#{myBean.selectedItem}">
  <c:forEach items="#{myBean.items}" var="item" varStatus="itemIndex">
    <f:selectItem itemValue="#{myBean.getValueAt( itemIndex.index ) }" itemLabel="#{myBean.getLabelAt( itemIndex.index )}"/>
  </c:forEach>
</h:selectOneMenu>
The Bitman
  • 1,279
  • 1
  • 11
  • 25
  • Just tried and nums gives me an error saying unexpected type Required: reference, found: double Any ideas? – Kilzrus Mar 26 '17 at 19:11
  • OK. I have modified `double` to `Double` – The Bitman Mar 26 '17 at 19:16
  • I have also tried Double but that doesn't work either. – Kilzrus Mar 26 '17 at 19:17
  • I get "incompatible types: inference variable T has incompatible bounds equality constriants: Double lower bounds: Integer, Double Where T is a type-variable T Extends Object declared in method asList(T....) – Kilzrus Mar 26 '17 at 19:18
  • I don't understand this, my normal array: private double list = {51.1511, 53.51351, 0.634343, 2.52555} shows and works perfectly and so does my currencies array if i put it under the value="" attribute but when i put one in the itemLabel it only shows the memory reference – Kilzrus Mar 26 '17 at 19:25
  • I have made some refactoring. Check it out. It worked for me with two different collection, not as object properties. – The Bitman Mar 26 '17 at 21:45