15

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();
    } 
}
Mitigy
  • 74
  • 2
  • 11
user1456170
  • 155
  • 1
  • 1
  • 7

5 Answers5

9

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, if 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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    On a JTextField, if a key typed event is consumed the character is not added to the field. But the other listeners declared are still called, they can check `event.isConsumed()` to decide if the event should be processed. Note that the listeners are actually called in the order they are declared. – Emmanuel Bourg Jun 07 '21 at 06:59
  • 1
    @EmmanuelBourg From my experience, the listeners were called LIFO order - so the last listener added can, in theory, it can consume the event. How event consumption is actually handled actually differs a lot based on the implementation of the individual dispatchers :/ – MadProgrammer Jun 07 '21 at 08:20
  • `java.awt.Component` has only one key listener and `addKeyListener` calls `AWTEventMulticaster.add(currentListener, addedListener)` to append a new listener. The composite listener created calls the old listener first and then the one added, so the listeners are notified in the order they were registered. – Emmanuel Bourg Jun 07 '21 at 08:27
  • Congratulations, you're talking about 1 type of core listener, I'm was, back in 2012, talking generally, particularly about those which make use of the `EventListenerList`, which orders the listeners in reverse order - I'm sorry I didn't make myself more clear – MadProgrammer Jun 07 '21 at 11:04
  • That's right, the listeners for the input events (keyboard event, mouse events, with the exception of drag events) are notified in the normal order, but all the other events (action events, change events, table model events, etc) are notified in the reverse order. `EventListenerList` doesn't actually store the listeners in reverse order, the components simply iterate over the list in reverse order. – Emmanuel Bourg Jun 07 '21 at 12:02
  • 1
    Again, didn't say that `EventListenerList` "stored" them in reverse order, I just said it "orders" them in reverse order - the internal mechanism should be, for the most part, implementation detail, it's how it appears to the caller which is the important detail. In my answer, I never made reference to the "internal" mechanism or `KeyEvent` or `KeyListener` in anyway, instead, I made a "abstract" observation. Because, as we can see, there is more then one event dispatching workflow within the API – MadProgrammer Jun 07 '21 at 22:11
4

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.

aviad
  • 8,229
  • 9
  • 50
  • 98
3

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.

UVM
  • 9,776
  • 6
  • 41
  • 66
  • Note that consuming a key pressed event doesn't prevent a JTextField from being updated, you want to consume the key typed event instead. – Emmanuel Bourg Jun 07 '21 at 07:06
2

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.

asgs
  • 3,928
  • 6
  • 39
  • 54
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
0

When the event is consumed, the source of the event (for example the JTextField which had the focus when the key was typed) will ignore the event.

If you look at the processKeyEvent method of the JComponent class:

protected void processKeyEvent(KeyEvent e) {
    boolean result;
    boolean shouldProcessKey;

    // This gives the key event listeners a crack at the event
    super.processKeyEvent(e);

    // give the component itself a crack at the event
    if (! e.isConsumed()) {
        processComponentKeyEvent(e);
    }

You can see that super.processKeyEvent(e) is called first, this dispatches the event to the listeners that were registered with component.addKeyListener(). The listeners are notified in the order they were originally registered, and they are all notified even if one of them consumes the event. The only thing that can stop an event from being processed by the remaining listeners is an uncaught exception.

After the listeners were notified, the component itself will process the event, but only if it hasn't been consumed by one of the listeners. For a JTextField, if a listener consumed the key typed event the field won't be updated (but consuming the key pressed event will have no effect).

Note that mouse events behave differently, an event consumed by one of the listeners is still processed by the component.

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76