aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes MouseListener error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseListener error" Watch "MouseListener error" New topic
Author

MouseListener error

Gabriella Hakansson
Greenhorn

Joined: Apr 19, 2010
Posts: 6

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..

Just trying to be a good programmer.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gabriella Hakansson
Greenhorn

Joined: Apr 19, 2010
Posts: 6
Wow! now it works just as it should. I only implemented all the MouseEvents. Thank you both soo much!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MouseListener error