1

I want to change my ActionListener of commandButton depending on the icon selected in advance:

        <p:dialog id="dialog" header="Add Memo" widgetVar="dialogMemo" resizable="false" >
        <h:form>
            <h:panelGrid columns="2" cellpadding="5">  
                <h:outputLabel for="commentInput" value="Comment:" />  
                <p:inputTextarea id="commentInput" value="#{dashboardBean.getCurrentComment()}" rows="6" cols="25" label="commentInput"/>
                <p:watermark for="commentInput" value="Enter your memo..."/>  
                <h:outputLabel for="selectShare" value="Share Memo: " />  
                <p:selectBooleanCheckbox id="selectShare" /> 

                <h:outputLabel for="choosePriority" value="Priority:" />  
                <p:selectOneMenu id="choosePriority" value="#{dashboardBean.currentPriority}" label="choosePriority">  
                    <f:selectItem itemLabel="Low Priority" itemValue="1" />  
                    <f:selectItem itemLabel="Medium Priority" itemValue="2" />  
                    <f:selectItem itemLabel="High Priority" itemValue="3" />  
                </p:selectOneMenu>

                <p:commandButton id="submitDialog" icon="ui-icon-check" value="Confirm" ajax='false' type="submit" action="#{dashboardBean.getLastMemo()}"/>
                <p:commandButton icon="ui-icon-close" onclick="dialogMemo.hide();" value="Cancel"/>
            </h:panelGrid>
        </h:form>
    </p:dialog>
    <p:layout fullPage="true">
        <p:layoutUnit id="leftPanel" position="west" size="250" header="My Memos" resizable="false" closable="false" collapsible="false">
            <h:form id="form"> 
                <p:commandButton id="addMemo" icon="ui-icon-plus" onclick="dialogMemo.show();" type="submit" action="#{dashboardBean.getEditControl}"/>  
                <p:dashboard id="dashboardId" model="#{dashboardBean.model}" binding="#{dashboardBean.dashboard}">
                </p:dashboard>  
            </h:form>
        </p:layoutUnit>           
</h:body>

When i click to command button (id="addMemo"), i want to change actionListener to commandButton(id="submitDialog").

I try that with:

public void getEditControl()
{
    UIViewRoot view = _context.getViewRoot();
    CommandButton button = (CommandButton) view.findComponent("submitDialog");

    System.out.println("I am ID ==== [ " + button.getId() +" ]");

}

But 'button.getId()' does't work.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Samolo
  • 75
  • 1
  • 13

2 Answers2

2

But 'button.getId()' does't work.

I gather that this is because the button is actually null and therefore the attempt to invoke the getId() method threw a NullPointerException? It's surprising that you stated the problem like that, "button.getId() doesn't work" instead of like "view.findComponent() returned null" (which has in turn the pretty obvious consequence that any attempt to access it would throw NullPointerException). If you had indeed no clue why it threw NullPointerException, then I strongly recommend to take a JSF pause and learn basic Java first.


Coming back to the concrete problem, the findComponent will return null when the client ID submitDialog doesn't exist in the component tree. Indeed, you have it inside a <h:form> which is a NamingContainer component. The button's client ID is more likely formId:submitDialog where formId is the ID of the button's parent form which you have to assign yet. An easy way to find out it is checking the ID of the generated HTML representation of the button.

See also this related question: How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar" Just substitute "ajax" in the answer with findComponent.

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

I am not sure that the way you do this is the right way. If you want to get component instance better way to do this will be not get it by id but add

<p:commandButton id="submitDialog" icon="ui-icon-check" 
value="Confirm" ajax='false' type="submit" 
action="#{dashboardBean.getLastMemo()}" binding="#{dashboardBean.component}" />

and that way the property in the bean will always have the component instance that you need. Note that the scope of bean where you put component binding should be view to avoid side affects.

trims
  • 453
  • 4
  • 9
  • The conditional expression causes a runtime exception because the expression does not represent a valid method expression. The binding doesn't solve OP's particular problem and makes it possibly worse if the bean is not in request scope. – BalusC Sep 09 '13 at 14:26
  • You are right. Method expression doesn't allow to use such expressions. My fault. I will remove it from the answer if you don't mind. But as far change of action listener on the component will be view scoped it will be better to have view scoped bean with binding attribute rather change of the component by getting it by id from view. – trims Sep 09 '13 at 15:57
  • 1
    In any case, using `binding` on a bean property is fishy and should really only be used if there's absolutely no other sensible way. – BalusC Sep 09 '13 at 15:58