Suppose I have a class that creates a window and attaches buttons to this window. That is why I need to create listeners for these buttons. And I decided that the listener would be alone. He will recognize which button was pressed.

But the question is this: What exactly does the getSource() method getSource() ?

 public class MyListener implements ActionListener{ @Override public void actionPerformed(ActionEvent ae) { //что хранится в переменной event? Object event = ev.getSource(); //code... } } 
  • 2
    This is written in the documentation. Before you create a question, you should first apply to it. - iksuy
  • @iksuy Let's start with the fact that this is a forum. Therefore, I can ask any questions related to programming. - SlandShow
  • 2
    In general, this is not a forum, but Q & A - there are different rules here, but you really have to ask the right to ask questions here - pavlofff
  • Of course, @SlandShow can, I didn’t write what I can’t, I just gave a recommendation that will help you grow as a professional. - iksuy

2 answers 2

getSource () returns a reference to the object from which the proof event came

    Just explicitly convert the getSource () object;

     JButton btn=(JButton) e.getSource(); 

    And then work like a button object. I understand that you are looking for an answer to this question. And getSourse () does return an event event source object.

    • Thanks, of course, but why? A lot of time has already passed since I asked this question. - SlandShow
    • The fact is that other young programmers may come across such a problem and this link)) So I helped them rather than you - Nikita