The following example prints output of the Mouse behavior (Entered, Pressed, Released, Clicked, Exited) as it happens.
It is also an attempt to consume the mouse behavior which is obviously done incorrectly.
When a mouse is clicked on the Frame, the Mouse Events : Pressed, Released and Clicked are processed.
Since the Pressed is the first Mouse Event being processed the consume() call was placed there expecting the Mouse Released and Pressed calls to NOT execute, but that did not happen.
Even in the other Mouse calls the code checks if the event is consumed via the isConsumed() call but that has no affect.
In what way is the consume() call being implemented incorrectly? Is it possible to simply process the Mouse Pressed call and none of the other Mouse Events?
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class MouseListenerExample extends JFrame implements MouseListener
{
Label l;
MouseListenerExample()
{
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
if (e.isConsumed())
return;
System.out.println("Mouse Clicked");
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e)
{
System.out.println("Mouse Entered");
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("Mouse Exited");
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{
System.out.println("Mouse Pressed");
e.consume();
System.out.println("Mouse Pressed - After consume()");
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
if (e.isConsumed())
return;
System.out.println("Mouse Released");
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}