MouseListener methods overridden but compiler says otherwise? [duplicate]
Asked Answered
M

1

6
        JTextArea messageDisplayArea=new JTextArea();
        messageDisplayArea.addMouseListener(new MouseListener(){
            public void mouseEntered(MouseEvent m){
                JOptionPane.showMessageDialog(null,"a");
            }
        });
        messageDisplayArea.addMouseListener(new MouseListener(){
            public void mouseExited(MouseEvent m){

            }
        });
        messageDisplayArea.addMouseListener(new MouseListener(){
            public void mouseClicked(MouseEvent m){

            }
        });
        messageDisplayArea.addMouseListener(new MouseListener(){
            public void mousePressed(MouseEvent m){

            }
        });
        messageDisplayArea.addMouseListener(new MouseListener(){
            public void mouseReleased(MouseEvent m){

            }
        });

This is part of my code, I've imported java.awt.* and java.awt.event.* and javax.swing.* but have NOT implemented the MouseListener interface. I've overridden all the methods from the MouseListener interface but the compiler throws 5 errors, all saying that class is not abstract and does not override abstract method in MouseListener. What am I doing wrong?

Mulct answered 28/9, 2015 at 11:0 Comment(0)
H
3

Try adding @Override.

With that code you shouldn't need to implement MouseListener.

Also instead of new Mouselistener() use new MouseAdapter()

Sample Code:

    JTextArea textArea = new JTextArea();
    textArea.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
        }
        @Override
        public void mouseEntered(MouseEvent e) {
        }
        @Override
        public void mouseExited(MouseEvent e) {
        }
        @Override
        public void mousePressed(MouseEvent e) {
        }
        @Override
        public void mouseReleased(MouseEvent e) {
        }
    });

Harbourage answered 28/9, 2015 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.