Sunday, March 1, 2020
Java Events and How They Work With Event Listeners
Java Events and How They Work With Event Listeners An event in Java is an object that is created when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers, creating the relevant event object. This behavior is part of Javas Event Handling mechanism and is included in the Swing GUI library.à For example, lets say we haveà a JButton. If a user clicks onà theà JButton,à aà button click event is triggered, the event will be created, and it will be sent to the relevant event listener (in this case, the ActionListener). The relevant listener will have implemented code that determines the action to take when the event occurs.à Note that an event source must be paired with an event listener, or its triggering will result in no action. How Events Work Event handling in Java is comprised of two key elements: The event source, which is an object that is created when an event occurs. Java provides several types of these event sources, discussed in the section Types of Events below.The event listener, the object that listens for events and processes them when they occur. There are several types of events and listeners in Java: each type of event is tied to a corresponding listener. For this discussion, lets consider a common type of event, an action event represented by the Java class ActionEvent, which is triggered when a user clicks a button or the item of a list.à At the users action, an ActionEvent object corresponding to the relevant action is created. This object contains both the event source information and the specific action taken by the user. This event object is then passed to the corresponding ActionListener objects method: à ââ¬â¹void actionPerformed(ActionEvent e) This method is executed and returns the appropriate GUI response, which might be toà open or close a dialog, download a file, provide a digital signature, or any other of the myriad actions available to users in an interface. Types of Events Here are some of the most common types of events in Java: ActionEvent: Represents a graphical element is clicked, such as a button or item in a list. Related listener:à ActionListener.ContainerEvent: Represents an event that occurs to the GUIs container itself, for example, if a user adds or removes an object from the interface.à Related listener:à ContainerListener.KeyEvent: Represents an event in which the user presses, types or releases a key.à Related listener:à KeyListener.WindowEvent: Represents an event relating to a window, for example, when a window is closed, activated or deactivated.à Related listener:à WindowListener.MouseEvent: Represents any event related to a mouse, such as when a mouse is clicked or pressed.à Related listener:à MouseListener. Note that multiple listeners and event sources can interact with one another. For example, multiple events can be registered by a single listener, if they are of the same type. This means that, for a similar set of components that perform the same type of action, one event listener can handle all the events. Similarly, a single event can be bound to multiple listeners, if that suits the programs design (although that is less common).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.