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?
It has five methods.
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.
MouseListener is an Interface and MouseAdapter is an implementation of that. You can use the MouseAdapter in every place that you use a MouseListener.
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) {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With