Introduction to OOP Chapter 22: The AWT and Swing : next previous audio real text

Combining Component and Listener in one Class

Sometimes, a better encapsulation results from combining inheritance from the component class with implementation of the listener. The component then becomes its own listener.
class ColorButton extends Button implements ActionListener {
	private Color ourColor;

	public ColorButton (Color c, String name) {
		super (name); // create the button
		ourColor = c; // save the color value
		addActionListener (this); // add ourselves as listener
	}

	public void actionPerformed (ActionEvent e) { 
			// set color for middle panel
		setFromColor (ourColor); 
	}
}
Intro OOP, Chapter 22, Slide 10