Docsity

Java GUI Programming: Understanding Event Handling, Listeners, and Event Classes, Slides of Computer Engineering and Programming

Partial preview of the text

Download Java GUI Programming: Understanding Event Handling, Listeners, and Event Classes and more Slides Computer Engineering and Programming in PDF only on Docsity!

GUI Programming in Java:

Event Handling

Event-Driven Programming

 Procedural programming is executed in

procedural order.

 In event-driven programming, code is

executed upon activation of events.

Selected User Actions

Source Event Type User Action Object Generated

Click a button JButton ActionEvent

Click a check box JCheckBox ItemEvent, ActionEvent

Click a radio button JRadioButton ItemEvent, ActionEvent

Press return on a text field JTextField ActionEvent

Select a new item JComboBox ItemEvent, ActionEvent

Select an item from a List JList ListSelectionEvent

Window opened, closed, etc. Window WindowEvent

Mouse pressed, released, etc. Any Component MouseEvent

Key released, pressed, etc. Any Component KeyEvent

Java AWT Event

Listener Interfaces

 ActionListener

 AdjustmentListener

 ComponentListener

 ContainerListener

 FocusListener

 ItemListener

 KeyListener

 MouseListener

 MouseMotionListener

 TextListener

 WindowListener

 ListSelectionListener

All are in the java.awt.event or javax.swing.event package

All are derived from EventListener in the java.util package

NOTE: any object that will respond to an event must implement a listener interface.

Handling Simple Action Events

Implementing the listener interface

Registering the frame to be a listener for action events generated by the two buttons

The method for responding to an Action event.

Handling Simple Action Events –

a closer look at the event-handling method

actionPerformed is a method required for all ActionListeners

An Event object’s getSource() method returns a reference to the Component object that generated the event.

Example with

named inner

classes, one for

listening to each

button

Inner class has direct access to all members (even private) of the outer class

Example with anonymous inner classes, one for listening to each button