Examples and practices described in this page dont take advantage of improvements introduced in later releases and might use technology no longer available.See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.Your first source of information for event should be the how-to section for the component in question.
Java Events Examples Code For HandlingEach components section shows code for handling the events most commonly needed when implementing the component.One of the topics includes information on using adapters and inner classes to implement event handlers. You might want to bookmark this section so you can easily find its quick-reference table. The interface the object must implement depends on the event it wants to receive. This article will show you three ways to get started with Java event handling. In this article, Ill present the concepts behind event-driven applications and look at various methods of event handling. Then, Ill discuss some generic techniques you can use to simplify the task of handling events. These graphic-intensive systems are driven by the user with events. In contrast, with the traditional programming model, the application controls program flow. ![]() Figure A Traditional program flow An example of this type of application is a command-line utility. Once the utility is invoked, it performs its function depending just on the commands given and according to the coded logic, independent of any external factors. Events do not adhere to this traditional approach because they occur outside of program control. When an event happens, the application is notified, causing the execution of a piece of code ( Figure B ). Usually, this notification involves calling a predefined procedure or function, passing it enough information to identify the event so that the application can perform its logic. Figure B Program flow with events Events may occur at any time with no predefined order, so a different programming model is required. ![]() Moreover, the application must be coded so that it does not depend on the order of the events. This programming model defines an event-driven application, such as an interactive application employing a graphical user interface (GUI). ![]() Furthermore, the user can press buttons, select options, and type something at any time and in any order. Basically, events are randomthey occur at the users discretion. An event is represented by a java.util.EventObject subclass that carries event information. At a minimum, they hold a reference to the object that generated the event (the source of the event), but each subclass defines additional information appropriate for the event. For example, a javax.swing.JButton object generates a java.awt.event.ActionEvent when pressed. It creates an instance of the java.awt.event.ActionEvent class, populates it with information about the event, including a reference to itself, and notifies the interested parties about the event. Event notification The notification mechanism involves calling a specific method of any objects that want to be notified about the event and passing the event object as a parameter. For an object to be notified about an event, it must implement a specific interface and be registered with the source of the event.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |