Junior — Middle
Can you explain what is meant by listeners in programming?
sobes.tech AI
Answer from AI
In programming, listeners are objects or methods that wait for certain events to occur and react to them. For example, in Java, a listener can be an interface that implements an event handling method, such as a button press or data change.
Example: in Java Swing, ActionListener is used to handle button presses:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button pressed!");
}
});
Thus, a listener allows separating event handling logic from the main code, making the program more modular and easier to maintain.