Controls and Listeners

Controls

The two systems of controls in the Java Class Library are AWT (Abstract Window Toolkit) and Swing. While AWT works well for simple applications, it gets complicated quickly. Although it is supposed to be platform independent, AWT must be debugged separately for the different platforms on which it is to run. Swing seems to be more stable and portable across platforms. It is also considered to be more powerful than AWT. A detailed examination of controls and event handling architectures is beyond the scope of this class. We will use AWT for controls on applets and Swing for controls on frame window applications.

Here is a table showing some controls and their listeners:

AWT Name Swing Name Listener Interface Detects
Button JButton ActionListener Clicked or enter key pressed
CheckBox JCheckBox ItemListener Selected or deselected
Component JComponent ComponentListener Moved, resized, hidden, shown
FocusListener Focus gained or lost
MouseListener Mouse pressed, released, moved, etc.
Container   ContainerListener Added or removed
ScrollBar JScrollBar AdjustmentEvent Moved
TextField JTextField ActionListener Enter key pressed
TextListener Text changed
Window JWindow WindowListener Opened, closed, iconified, etc

 

Listeners

An interface is a set of headers for methods. A class that implements this interface agrees to supply bodies for these methods. The most commonly used interfaces in the Java Class Library are the Listener Interfaces. The listener methods that must be implemented are the event handlers that executed when the listener detects an event.

Here some listener interfaces and the methods (event handlers) that must be implemented for them:

Listener Interface Listener Method
ActionListener actionPerformed
AdjustmentListener adjustmentValueChanged
ComponentListener componentHidden
componentMoved
componentResized
componentShown
ContainerListener containerAdded
containerRemoved
FocusListener focusGained
focusLost
ItemListener itemStateChanged
MouseListener mouseClicked
mouseEntered
mousePressed
mouseReleased
MouseMotionListener mouseDragged
mouseMoved
TextListener textValueChanged
WindowListenerwindowActivated
windowClosed
windowClosing
windowDeactivated
windowDeiconified
windowIconified
windowOpened