• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't add ActionListener  RSS feed

 
Oleksandr Velychko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, could somebody explain me how to add ActionListener? Have a programm with added interface of ActionListener, and overrided function in another class, but after creating ActionListener instance couldn`t assign it to created JButton instance...





Eclipse Error
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the error message with your question, not id DropBox.

All you need is the message and the line causing the problem.
 
Oleksandr Velychko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Post the error message with your question, not id DropBox.

All you need is the message and the line causing the problem.


“The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments” in lines 12 and 15 in class UI.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of your interface code.

ActionListener is already an interface defined in the JDK. You don't need to define it yourself.

You can just use the interface.

Also add all the proper imports to your other two classes.
 
Oleksandr Velychko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Get rid of your interface code.

ActionListener is already an interface defined in the JDK. You don't need to define it yourself.


OMG, thank a lot! Why i didn't think about it before....
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You can reduce your action listener to a λ in Java8, and you might get away without the importIt is probably better to put the required action into a method than to write a long actionPerformed method. I have a little rule of thumb for whether to write a public class, a private class or an anonymous class: look here. Of course now you have functional interfaces (ActionListener is a functional interface), you can replace anonymous classes with λs.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!