40 likes | 250 Views
Chapter 17: Handling Events. Basics of Events Handling. The concept of event-driven programming in contrast with procedural programming Classes of Events: Low-level events such as moving the mouse of closing a window Semantic events (component-related events) such clicking a button
E N D
Basics of Events Handling • The concept of event-driven programming in contrast with procedural programming • Classes of Events: • Low-level events such as moving the mouse of closing a window • Semantic events (component-related events) such clicking a button • Almost all event classes inherit from AWTEvent (found in java.awt) • You can handle events in two ways: • Using listeners (preferred) • Enabling events directly for objects • Example of the second approach: 1. Closing a Window\Sketcher • All event listener interfaces extend the interface java.util.EventListener
Basics of Events Handling (Cont.) • You can implement a low-level event listener. Ex: 2. Low-level Event Listener • Implementing am adapter in the above case will avoid unnecessary code. Ex 3. Implementing an Adapter Class • To handle semantic events, there are three main classes: • ActionEvent: produced by buttons, menus, & text • ItemEvent: produced by JToggleButtons, JCheckBox, JMenuItem, etc. • AdjustementEvent: JScrollbar • Example: Lottery Applet • Handling low-level and semantic events. Ex: Adding a Mouse Listener
Handling Semantic Events in Applications • Adding event listeners to the color and element menus. Ex: 6. Color Menu Listeners • The final form of the SketchFrame (9) • Adding Actions & tools bar • Tool tips • Disabling actions • Questions !!