This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseListener error

 
Gabriella Hakansson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't know whats wrong, but when i compile i get the errormessage:
test.java:9: test is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener
public class test implements MouseListener{


Do you have to add a box on top of the JLabel for the mouse to click in ? How is that done?

I feel a bit stupid now..
 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the section from the Swing tutorial on How to Write a Mouse Listener. You need to implement "all" the methods of the mouse listener, not just one. You only implemented mouseClicked".

Of course the easier solution is to use a JButton and then add an ActionListener to the button. You can use setBorderPainted(false) to make the button look more like a label.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most event listener interfaces have matching abstract classes that have empty body implementations for all methods. For MouseListener that matching class is MouseAdapter. Extending this class instead of implementing MouseListener allows you to implement only a few methods instead of having to implement all of them.
 
Gabriella Hakansson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! now it works just as it should. I only implemented all the MouseEvents. Thank you both soo much!
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic