Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

actionListener doesn't work, but the method does

I have tested my method to add a value to the database and it works fine. However, when I try to use the method as an actionListener in a command button it doesn't work. Could anyone please explain what it is I have wrong? This is the method:

public void addToNetwork() {

    MindmapNode currentNode = this.selectedNode;

    this.user.setConnections( this.user.getConnections() + "-" + currentNode.getLabel() );
    this.userConn = Arrays.asList(this.user.getConnections().split("-"));
    this.setUserdetails( this.user );
}

And this is the xhtml:

<p:mindmap value="#{mmBean.root}" style="width:100%;height:600px" id="mindmap">
    <p:ajax event="select" listener="#{mmBean.onNodeSelect}" immediate="true"      
         id="oneClick" oncomplete="options.show()"  />
/p:mindmap>            


<p:dialog widgetVar="options">

    <p:panelGrid  id="optionspanel" columns="2">
        <h:outputText value="Add this user to your network?"/>
        <p:commandButton value="Add" update="mindmap" actionListener="#{mmBean.addToNetwork}" onclick="options.hide()" id="btnAdd"/>
    </p:panelGrid>

</p:dialog> 

This is the method for onNodeSelect:

public void onNodeSelect(SelectEvent event) {
    MindmapNode node = (MindmapNode) event.getObject();
    this.setSelectedNode(node);
}

Note that I easily force the add when they click, but I want to offer the user a bunch of other options as well, eventually.

Could this be done by, say, setting a property within addToNetwork to being true when they click add? Basically I'm thinking of something like remoteCommand? I tried having the first command set a connect-property to true and then the remoCommand calling the method with an if loop to connect if that property was set to true. That didn't work.

I suspect the problem is telling with maintaining in the object which node is the selectedNode, but I'm not sure. Any help would be great!

Edit: The answer was to store the nodelabel in the database and then access it again from there. Quite simple, really. I think the object was being destroyed sometime between the selectedNode value being set and being called, but I'm not 100% sure on that.

like image 367
AodhanOL Avatar asked Feb 15 '26 10:02

AodhanOL


1 Answers

In contrary to the action method, the actionListener method must take an ActionEvent argument.

You have 2 options:

  1. Use action instead.

    <p:commandButton ... action="#{mmBean.addToNetwork}" />
    
  2. Add that argument, even though you never use it.

    import javax.faces.event.ActionEvent;
    
    // ...
    
    public void addToNetwork(ActionEvent event) {
        // ...
    }
    

See also:

  • Differences between action and actionListener
like image 186
BalusC Avatar answered Feb 17 '26 01:02

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!