0
votes

I'm dealing with something that seems to be a trivial task but haven't found a solution: How can I access the text on a RichSelectOneChoice? I've only found the values with richSelectOneChoice.getValue() and valueChangeEvent.getNewValue()

But, how is it possible to access the actual text?

My last attempt was this:

private RichSelectOneChoice selClaim;

public void claimTypeVCL(ValueChangeEvent ve){
    Map s = selClaim.getAttributes();
    Object ss = s.get(ve.getNewValue());
    System.out.println(ss);
}

At the moment the console output is null for the corresponding value, no matter what the choice is.

The ADF component bound to the RichSelectOneChoice object is created as a component with inner elements.

I've also tried the solution proposed by Frank Nimphius here https://community.oracle.com/thread/1050821 with the proper object type (RichSelectOneChoice) but the if clause doesn't execute because the children are not instanceof RichSelectOneChoice as suggested but rather javax.faces.component.UISelectItem and this class doesn't include the getLabel() method and running the code actually throws a wide range of errors related either to casting an object to the target type or null pointers when trying to access the label.

1
did you try with UISelectItem object's getAttribute(key) method? Like selectItem.gettAttribute("description") - Veselin Davidov
The UISelectItem class doesn't have a getAttribute() method, only a getAttributes() method, which returns a Map<String, Object>. I will try this. - codeSwim

1 Answers

0
votes

Solved it using the UISelectionItem object and its getItemValue() and getItemLabel() methods instead of getLabel() or getValue(), the latter of which was available but didn't render the expected result.

The working code looks like this:

public String selectedOptionStr;
public void socClaimTypeVCL(ValueChangeEvent ve){
    selectedOptionStr = "";
    RichSelectOneChoice sct = (RichSelectOneChoice)ve.getSource();
    List childList = sct.getChildren();
    for (int i = 0; i < childList.size(); i++) {
        if (childList.get(i) instanceof javax.faces.component.UISelectItem) {                
            javax.faces.component.UISelectItem csi = (javax.faces.component.UISelectItem) childList.get(i);
            if (csi.getItemValue().toString() == ve.getNewValue().toString()) {
                selectedOptionStr = csi.getItemLabel();
            }
        }
    }
}