Java - How can I difference between two or more JButtons?

I'm actually doing a project and I have a ArrayList of JButtons with an ActionListener added. And I don't know how to difference between them in my ActionPerformed method. The buttons have all the same title but any button do the same action.

Upvotes: 1

Views: 135

Answers (1)

Andres
Andres

Reputation: 10717

In your actionPerformed method do something like this :

public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == jButton1){
      //perform action when jButton1 clicked
    }
    if(e.getSource() == jButton2){
      //perform action when jButton2 clicked
    }
//So on and so forth
}

Upvotes: 2

Related Questions