It depends. If you want to use an ActionListener for several user interface components: a button, menu item, ... then it would be advisable to do this in a separate class. In addition, if the code inside the actionPerformed method has many lines, it makes sense to do it separately.
Otherwise, if there are not many user interface components in your class, you can define ActionListener as an anonymous implementation and directly attach it to your component.
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
source share