Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does e.consume() do in java

Tags:

java

I search online and get no clear explaination about the use of e.consume() which is often used in java KeyEvent Handle.Like the follow Code.

public void keyTyped( KeyEvent e ) {
    char c = e.getKeyChar();
    if ( c != KeyEvent.CHAR_UNDEFINED ) {
        s = s + c; 
        repaint();
        e.consume();
    } 
}
like image 462
user1456170 Avatar asked Sep 23 '12 07:09

user1456170


4 Answers

Look at the documentation: The KeyEvent inherits consume method from InputEvent class. The consume method consumes this event so that it will not be processed in the default manner by the source which originated it.

like image 162
aviad Avatar answered Nov 05 '22 09:11

aviad


Consume function is responsible for not processing the KeyListeners code during some specific kind of events happen. For example : If i want to make a textfield in java such that it will only respond when digits are pressed, then I can use the consume method to consume (Not process the keyevents which were not caused due to the pressing of digits) such events.

like image 28
UVM Avatar answered Nov 05 '22 09:11

UVM


From the JavaDocs

Consumes this event so that it will not be processed in the default manner by the source which originated it.

Essentially what it means is you don't want the event to be dispatched to or handled by any further event listeners.

Although, generally speaking, this is dependent on the implementation of the individual listeners

like image 8
MadProgrammer Avatar answered Nov 05 '22 09:11

MadProgrammer


It is a method of the AWTEvent Class. It is used to consume this event, if it can be consumed.

Did you check AWTEvent.consume()?

Furthermore, look at How Does consume() Work? for explanation about what exactly consume does.

like image 2
Kazekage Gaara Avatar answered Nov 05 '22 09:11

Kazekage Gaara