Intact Abode
Intact Abode

Reputation: 392

How to Perform Multiple Action on Single Click in Java Swing

I have a Question on performing other buttons action with single button click. Some example code for three buttons:

JButton a = new JButton("a");
a.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // Action of a is Here                   
  }
});

JButton b = new JButton("b");
b.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // Action of b is Here                   
  }
});

Those should come together, like:

JButton c = new JButton("c");
c.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // Action of c is Here
    // Action of a 
    // Action of b              
   }
});

In the above example i have three buttons a,b,c with its own action; but as you can see, C also has to run the actions of A and B. What are good ways to address this?

Upvotes: 4

Views: 4560

Answers (4)

AxelH
AxelH

Reputation: 14572

1) Methods

Use methods for each action and call those in the ActionListener.actionPerformed

public void methodA(){}
public void methodB(){
    methodA();
}

2) Action instance

You could create your own classes of ActionListener to perform the actions

First action :

class ActionA implements ActionListener{
     public void actionPerformed(ActionEvent e) {
          ...
    }
}

An improved action

class ActionB extends ActionA{
    public void actionPerformed(ActionEvent e) {
           super.actionPerformed(e);  //Will call the first action
           ...
    }
}

This is limited since you can't have multiple extends but is also a nice solution

3) Click

Last but I don't like it, use AbstractButton.doClick to dynamicly click on other buttons.

4) Add multiple action

Just notice that the methods is not a setActionListener but a addActionListener meaning that it will accept multiple ActionListener.

So define create two instances

ActionListener listenerA = new ActionLisener ..
ActionListener listenerB = new ActionLisener ..

buttonA.addActionListener(listenerA);

buttonB.addActionListener(listenerB);

buttonC.addActionListener(listenerA);
buttonC.addActionListener(listenerB);

With a small test, I notice that the actions are execute in the order B -> A (might not be a generality).

As said in comment, this should be us knowing the risk, this will . If an action failed because of an exception, should the next one be executed ? By default it won't because the process will not hide exceptions.

I would restrict this solution to GUI management like reseting fields, disabling, ... that could be use in different buttons.

Upvotes: 2

GhostCat
GhostCat

Reputation: 140613

The other answers are all correct, but there is one important aspect missing here: be careful about dong "too many things" on the AWT event dispatcher thread.

Meaning: when a button is clicked, an event gets created, and the UI framework uses that special thread to trigger the registered listeners. If one of the listeners now decides to do a intensive computation ... the UI event threads stays busy doing "that". And while doing "that thing"; this thread isn't available to dispatch any other UI event.

So, this is "not only" about creating methodA(), methodB(), methodC() and invoking them in your third action listener. It is also about understanding if combining multiple calls becomes subject to "I should better run those things in a separate thread; to not block the event dispatcher thread".

In that sense: the other answers tell you where to go from here; but be really careful about the "amount of activity" that your "joined actions" button is about to create!

Upvotes: 4

Stimpson Cat
Stimpson Cat

Reputation: 1508

Create 3 methods for each button indepently from the actionListeners action Perform method and call them from the actionPerfomed methods:

private void btnAClicked(){};
private void btnBClicked(){};
private void btnCClicked(){};


JButton c = new JButton("c");
    c.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           btnCClicked();
           btnAClicked(); 
           btnBClicked();              

        }
    });

Upvotes: 1

Stefan Lindner
Stefan Lindner

Reputation: 329

Whatever you want to do on Button click a, you can put in a method and call it from wherever you want.

public void methodForA(){
   // do here what you want
}

You can call this now in the methods you want it to call from. In your case from button click A and button click C

JButton a = new JButton("a");
a.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        methodForA();
    }
 });

// and also in your c-Button
JButton c = new JButton("c");
c.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
          // Action of c is Here
          methodForA(); 
   }
});

Upvotes: 1

Related Questions