Dan
Dan

Reputation: 597

how to have multiple ActionListeners for a single button

I have a wizard based software, with many panels. There can be different paths to get the last screen. So each path has its own controller to calculate data. In the final panel, where all paths ends there, there is a button to finalize data. That button has an action listener that is called in all paths's controllers. So when the button is pressed, it basically runs all the controllers and obviously errors come up. Here is the method for the button action listener in the final panel:

public void destinationNextButtonListener(ActionListener listenForDestinationNextButton){
        localDestinationNext.addActionListener(listenForDestinationNextButton);
}

And each controller's contractor I initialize the Action Listener like this:

Controller 1:

{
.
.
this.localDestinationChooser.destinationNextButtonListener(new destinationNextButtonListener());
}

class destinationNextButtonListener implements ActionListener{...}

So if there are 2 controllers, when the button is pressed, all of the controllers start working. I am looking for a logic or something that the action listener decides which controller it must listen to.

Any idea?

Upvotes: 0

Views: 101

Answers (2)

blurfus
blurfus

Reputation: 14031

I think you should use inheritance for more flexibility in your design (and remove complexity).

The concept would be that each screen should be controlled by its own controller.

All other screens would have their specific controllers as well (which could and even should inherit from the last screen one)

With that in place, the behaviour of the button on the last screen can the same for all wizard pages regardless of which implementation was used to get there (since they all inherit from the parent one)

Upvotes: 1

Ungeheuer
Ungeheuer

Reputation: 1443

The best idea (that I know of, and I'm not a Java guru) would be to put in boolean statements if the user goes down that path. Such that in the ActionListener definition boolean alphaPath, which should be instantiated as false at the beginning of your program and should be a class variable, not local, is then set to true. Then in the final ActionListener use an if else-if (extend as necessary, i.e. add another else-if) statement set to find which path the user went down and execute the proper code such that

if(alphaPath == true)
{
    //execute proper code
}

else if(betaPath == true)
{
    //execute beta code
}

It would be great if you could show us more code so that we can better understand your program. I am basing my solution off of the way I believe your program is coded. Seeing actual code would be great. When you edit and add the code to your question, add a message in the comment section on this answer and I will edit my solution.

Upvotes: 1

Related Questions