Listeners / Event Handlers
The whole point of using GUI's of course is to make a program more interactive for the user.
In event-driven programming, an event represents some user action that results in a response, such a clicking a button with a mouse. A listener object "waits" for an event and then responds in some way. Here's an example:
In event-driven programming, an event represents some user action that results in a response, such a clicking a button with a mouse. A listener object "waits" for an event and then responds in some way. Here's an example:
This would produce a JFrame with a button containing the String “Change Color”:
If the button is clicked on, the listener (that is, the myButtonListener class) responds by running the actionPerformed( ) function, setting the background color to blue: