How does, for example, a button in Java actually listen for an event to occur? I understand button events are handled once they are clicked in a special EDT (Event Dispatching Thread). But how without "busy waiting" does the button know that it has been actually clicked. The only way I understand this is possible if in a separate thread hidden from the user such as the Event Dispatching Thread there is a constant polling every so often, maybe every few milliseconds to check to see if it was clicked. On top of this, how does a button click invoke code?
I assume people are going to suggest it is the Observer Pattern at work here, but from the examples I have seen, the user more or less explicitly notifies the observers so it's almost no different than calling just a regular method.
s.setName("IceCream");
public void setName(String name) {
this.name = name;
setChanged();
notifyObservers();
}
and then the update() method is called. This isn't the Observer Pattern from scratch but using the Observable and Observer classes.
Let me know if anything needs to be clarified
This question is sort-of-similar to my last question regarding how to constantly poll a condition without busy waiting. How do you pause a thread until a condition becomes true without busy waiting?