Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kimi

macrumors regular
Original poster
Ok, working on a simple applet for a class at uni (basically following a step by step) and I can't work out what's wrong. The applet is ment to change the colour of a box when the mouse is clicked, which I can do fine, it's just the start that's buggered me.

This is what I've got, I only added in the "implements MouseListener" and "addMouseListener(this);" parts.

Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class HelloWorld extends Applet implements MouseListener{
private String message = "Hello World!";
    public void init() {
addMouseListener(this);
    }

    public void paint(Graphics g) {

(other stuff goes here, which works fine)

When I compile it I get an error on the "public class HelloWorld extends Applet implements MouseListener{" line, saying:

HelloWorld is not abstract and does not override abstract method mouseReleased(java.awt.event.MouseEvent) in java.awt.event.MouseListener

And I've got no idea what that means. Any help?
 

AndyS

macrumors newbie
Nov 13, 2006
4
0
hi Kimi,

first some text about an interface:
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

This means, that you HAVE to implement all the methods of the interface in the class which implements it. An empty method will do the trick.

Here some more explanation:
http://en.wikipedia.org/wiki/Interface_(Java)
 

nesbitt_a

macrumors 6502
Nov 1, 2003
313
0
Ok, working on a simple applet for a class at uni (basically following a step by step) and I can't work out what's wrong. The applet is ment to change the colour of a box when the mouse is clicked, which I can do fine, it's just the start that's buggered me.

This is what I've got, I only added in the "implements MouseListener" and "addMouseListener(this);" parts.

Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class HelloWorld extends Applet implements MouseListener{
private String message = "Hello World!";
    public void init() {
addMouseListener(this);
    }

    public void paint(Graphics g) {

(other stuff goes here, which works fine)

When I compile it I get an error on the "public class HelloWorld extends Applet implements MouseListener{" line, saying:

HelloWorld is not abstract and does not override abstract method mouseReleased(java.awt.event.MouseEvent) in java.awt.event.MouseListener

And I've got no idea what that means. Any help?

The problem is that because you're implementing the MouseListener Interface, and because MouseListener has the methods MouseClicked, MouseEntered, MouseExited, MousePressed, MouseReleased, Java is expecting you to provide an implementation of each of these methods (if you are to fully implement MouseListener). If you only want to implement some (or none) of the methods in the MouseListener interface, you can change your class declaration to be 'Abstract' - telling Java that you intentionally didn't implement certain methods;

public abstract class HelloWorld extends Applet implements MouseListener
{
....
}


Edit: Andy S beat me to it.
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
If you use MouseAdapter instead of MouseListener it's all the same but you don't need the empty methods.
 

AndyS

macrumors newbie
Nov 13, 2006
4
0
If you use MouseAdapter instead of MouseListener it's all the same but you don't need the empty methods.

This is not correct, since Java Ed.5.0 it is MouseInputAdaptor. You can indeed have a inheritance structure, BUT not multiple inheritence.

So in this particullary case implementing the interface is the only way, because there is already an extended Applet. (it's not C++ ;) )
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
This is not correct, since Java Ed.5.0 it is MouseInputAdaptor. You can indeed have a inheritance structure, BUT not multiple inheritence.

Good point, I forgot about that.

So in this particullary case implementing the interface is the only way, because there is already an extended Applet. (it's not C++ ;) )

It's not the only way. I think you'll find most people in industry would do it like this (anonymous subclass):

Code:
public void init()
{
    addMouseListener(new MouseInputAdapter() { 
        public void mouseClicked(MouseEvent e) {
            handleClick(e);        
        }
    });
}

void handleClick(MouseEvent e)
{
    // ...
}
 

AndyS

macrumors newbie
Nov 13, 2006
4
0
That is indeed a possibility, but I have noticed that you then easily are copying code and writing it double. But that is a personal opinion of course.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.