Noob Java help please

Discussion in 'Mac Programming' started by Kimi, Nov 13, 2006.

  1. Kimi macrumors regular

    Joined:
    Oct 29, 2005
    Location:
    Newcastle, England/ Stirling Scotland
    #1
    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?
     
  2. AndyS macrumors newbie

    Joined:
    Nov 13, 2006
    #2
    hi Kimi,

    first some text about an interface:
    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)
     
  3. nesbitt_a macrumors 6502

    Joined:
    Nov 1, 2003
    #3
    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.
     
  4. Kimi thread starter macrumors regular

    Joined:
    Oct 29, 2005
    Location:
    Newcastle, England/ Stirling Scotland
    #4
    Thanks, that's got it fixed. Justed added "public void mouseClicked(MouseEvent e) { }" as that's the one I didn't use and it's now got passed that error.
     
  5. aLoC macrumors 6502a

    Joined:
    Nov 10, 2006
    #5
    If you use MouseAdapter instead of MouseListener it's all the same but you don't need the empty methods.
     
  6. AndyS macrumors newbie

    Joined:
    Nov 13, 2006
    #6
    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++ ;) )
     
  7. aLoC macrumors 6502a

    Joined:
    Nov 10, 2006
    #7
    Good point, I forgot about that.

    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)
    {
        // ...
    }
    
     
  8. AndyS macrumors newbie

    Joined:
    Nov 13, 2006
    #8
    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.
     

Share This Page