How to perform multiple single click actions in Java Swing

I have a question about performing another action buttonswith one click button. Sample 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, for example:

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,cwith my action; but, as you can see, C should also run actions A and B. What are some good ways to solve this problem?

+4
source share
4 answers

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

: , . ... , "". ""; - .

, " " A(), methodB(), methodC() . , " , ".

: , ; " ", " " !

+4

1)

ActionListener.actionPerformed

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

2)

ActionListener

:

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

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

, ,

3)

, , AbstractButton.doClick, .

4)

, setActionListener, a addActionListener , ActionListener.

,

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

buttonA.addActionListener(listenerA);

buttonB.addActionListener(listenerB);

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

, B → A ( ).

, , . - , ? , .

, , ,... .

+2

, , "a", , .

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

, . A 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(); 
   }
});
+1

Create 3 methods for each button, regardless of the actionListeners action. Run the 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();              

        }
    });
+1
source

Source: https://habr.com/ru/post/1676982/


All Articles