Results 1 to 3 of 3
  1. #1
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

    Post MouseListener Error

    Hey im Having a problem with my MouseListener method override..its saying I did not override the method mouseExited in my class..but i clearly did..Can anyone help out my situation :( I have a different class with the main method which tests this code and has the setVisible(true); method. The error im getting is Uncompilable source code - mouseevent.MouseEvent.classHandler is not abstract and does not override abstract method mouseExited(java.awt.event.MouseEvent) in java.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    public class MouseEvent extends JFrame{
        private JLabel statusBar;
        private JPanel mousePanel;
        
        public MouseEvent(){
            super("Area");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(350,350);
            
            mousePanel = new JPanel();
            mousePanel.setBackground(Color.WHITE);
            add(mousePanel, BorderLayout.CENTER);
            
            statusBar = new JLabel("Do something....");
            add(statusBar,BorderLayout.SOUTH);
            
            classHandler handler;
            handler = new classHandler();
            mousePanel.addMouseListener(handler);
            mousePanel.addMouseMotionListener(handler); 
        }
        
        private class classHandler implements MouseListener, MouseMotionListener{      // saying mouseExited has not been overridden.
            public void mouseClicked(MouseEvent event){
                statusBar.setText(String.format("You clicked at %d,%d", 
                        event.getX(), event.getY()));
              }
            public void mousePressed(MouseEvent event){
                statusBar.setText("You pressed down the mouse");
            }
            public void mouseReleased(MouseEvent event){
                statusBar.setText("You released the mouse");
            }
            public void mouseEntered(MouseEvent event){
                statusBar.setText("You entered the area");
                mousePanel.setBackground(Color.ORANGE);
            }
           
            public void mouseDragged(MouseEvent event){
                statusBar.setText("You are dragging the mouse");
            }
            public void mouseMoved(MouseEvent event){
                statusBar.setText("You moved the mouse");
            }
            public void mouseExited(MouseEvent event){
                statusBar.setText("You have left the area");
            }
        
        } 
    }
    Last edited by DarrylBurke; 12-13-2012 at 01:44 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default Re: MouseListener Error

    That's what you get for naming your class the same as an existing JDK class, moreover a JDK class that you need to use. Actually, none of the MouseListener methods are implemented; they all require a parameter of type java.awt.event.MouseEvent whereas in the context of your compilation unit -- your source code -- any unqualified MouseEvent will be treated as a reference to an instance of the containing class, also (badly) named MouseEvent.

    Also, there's absolutely no need for this class to extend JFrame, as it isn't in any way a specialized JFrame, just an ordinary one -- so it could as well contain a JFrame, either as an instance field or as a (method-)local variable. Favor composition over inheritance.

    db
    Last edited by DarrylBurke; 12-13-2012 at 01:45 PM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DaveMc is offline Member
    Join Date
    Dec 2012
    Location
    Kerry, Ireland
    Posts
    8
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 11-07-2012, 12:44 PM
  2. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  3. Replies: 9
    Last Post: 09-21-2010, 04:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •