1

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();  
    }  
}
Unhandled Exception
  • 1,427
  • 14
  • 30
  • 4
    They are distinct events, you are only consuming the "Mouse Pressed" one . Also see : https://stackoverflow.com/questions/12550548/what-does-e-consume-do-in-java – Arnaud Oct 18 '19 at 12:11
  • Thanks, I read a number of link and they were pretty much the same simply stating it "consumes" the event but doesn't really clarify. One link you provided did seem to mention that it consumes the mouse events going to the keyboard event which did help to clarify it wasn't intended for other mouse events. – Unhandled Exception Oct 18 '19 at 16:07

1 Answers1

2

As @Arnoud said in the comments: These MouseEvents are all different Events. If you .consume() one, it doesn't mean the following events are consumed, too.

Is it possible to simply process the Mouse Pressed call and none of the other Mouse Events?

Yes, of course ;)

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){}  
    public void mouseEntered(MouseEvent e){}  
    public void mouseExited(MouseEvent e){}  
    public void mouseReleased(MouseEvent e){}

    public void mousePressed(MouseEvent e) {  
        System.out.println("Mouse Pressed");
        l.setText("Mouse Pressed");  
    }  

    public static void main(String[] args) {  
        new MouseListenerExample();  
    }  
}
bkis
  • 2,530
  • 1
  • 17
  • 31
  • Thanks, but I actually have code in the other Mouse Event that does need to execute, but just not in this once scenario. – Unhandled Exception Oct 18 '19 at 16:05
  • I did set a boolean within the first MouseEvent and just checked it in the subsequent mouse events to determine whether or not to simply return or continue processing. – Unhandled Exception Oct 18 '19 at 16:38
  • @UnhandledException If that worked for you, you could answer your own question here - to close the case, you know? :) – bkis Oct 21 '19 at 12:01