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.