Get button name from ActionListener in java ?

Here is the top answer to Get button name from ActionListener ?

JButton btnClear = new JButton("clear");
btnClear.addActionListener(this);
btnClear.setName("clear");
public void actionPerformed(ActionEvent e) {
   JButton o = (JButton)e.getSource();
   String name = o.getName();
   if (name == "clear")
   {
       euroMillText.setText("");
   }
   else if (name == "eumill")
   {
       getLottoNumbers();
   }
}   

Leave a Comment