Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MouseListener methods overridden but compiler says otherwise? [duplicate]

        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?

like image 407
SirVirgin Avatar asked Sep 28 '15 11:09

SirVirgin


People also ask

When a class implements MouseListener Interface how many methods it must override?

It has five methods.

What is the difference between MouseListener and MouseMotionListener?

What are the differences between a MouseListener and a MouseMotionListener in Java? We can implement a MouseListener interface when the mouse is stable while handling the mouse event whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event.

What is the difference between MouseAdapter and MouseListener?

MouseListener is an Interface and MouseAdapter is an implementation of that. You can use the MouseAdapter in every place that you use a MouseListener.


1 Answers

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) {
        }
    });

like image 119
Rubydesic Avatar answered Sep 17 '22 12:09

Rubydesic