Tips & Tricks
Dacj 2-2 b
Like this presentation? Why not share!
Intelligence Applied Autumn edition
BCG Business Strategic Model - The ...
Legal Shorts 10.04.15 including ris...
by Sheela Mistry
Corporate Social Responsibility
by Bharathiar Univer...
my Design profile
Email sent successfully!
Show related SlideShares at end
Dacj 2-2 b
, Software Developer, Web Developer
May 05, 2012
Comment goes here.
12 hours ago
Are you sure you want to
Your message goes here
Be the first to comment
Be the first to like this
Number of Embeds
No notes for slide
Dacj 2-2 b
1. Event-handlingPre-Assessment Questions 1. Identify the package that defines the JApplet class in Java. a. javax.swing b. java.awt c. java.io d. java.net 2. Which method suspends the execution of an applet? a. init() b. start() c. stop() d. destroy() ©NIIT Java Programming Lesson 2B / Slide 1 of 27
Event-handlingPre-Assessment Questions (Contd.) 3. Which class enables you to create a drop-down menu in Java? a. List b. Choice c. Label d. TabbedPane 4. Identify an intermediate-level Swing container. a. JPanel b. JFrame c. JDialog d. JComboBox ©NIIT Java Programming Lesson 2B / Slide 2 of 27
Event-handlingPre-assessment Questions (Contd.) 5. Which layout manager allows specified components to span multiple rows and columns? • FlowLayout manager • BorderLayout Manager • GridLayout Manager • GridBagLayout Manager ©NIIT Java Programming Lesson 2B / Slide 3 of 27
Event-handlingSolutions to Pre-assessmentQuestions • a. javax.swing • c. stop() • b. Choice • a. JPanel • d. GridBagLayout Manager ©NIIT Java Programming Lesson 2B / Slide 4 of 27
Event-handlingObjectives • In this lesson, you will learn about: • Event-handling in Java • Event Classes • EventListener Interfaces • Adapter Classes ©NIIT Java Programming Lesson 2B / Slide 5 of 27
Event-handlingEvent-Handling in Java • An object that describes a change of state in a source component is called an event. • The source components can be the elements of the Graphical User Interface (GUI). • Events are supported by the classes and interfaces defined in the java.awt.event package. • Identifying the Source Of Events: • An event source is an object that generates an event. • An event source registers some listeners, which receive notifications about a specific type of event generated by that particular event source. • All the registered listeners are notified about the generation of an event and receive a copy of the event object. • This is known as multicasting the event. • Some sources allow only single listener to register. • This is known as unicasting of event. ©NIIT Java Programming Lesson 2B / Slide 6 of 27
Event-handlingEvent-Handling in Java (Contd.)• Various event sources and the types of events they generate: Event Source DescriptionCheckbox Creates an item event when a check box is selected or deselected.Button Creates an action event when a button is pressed.List Creates an action event when an item is double-clicked; creates an item event when an item is selected or deselected. ©NIIT Java Programming Lesson 2B / Slide 7 of 27
Event-handlingEvent-Handling in Java (Contd.) Event Source Description Scrollbar Creates an adjustment event when a scroll bar is scrolled. Text components Creates a text event when a text character is entered in the text component. Window Creates a window event when a window is activated, deactivated, opened, closed, or quit. ©NIIT Java Programming Lesson 2B / Slide 8 of 27
Event-handlingEvent-Handling in Java (Contd.) • Event Listeners and Event Handlers • An event listener listens for a specific event and is notified when that specific event occurs. • An event listener registers with one or more event sources to receive notifications about specific types of events and processes the events. • An event-handler is called by the event listener whenever a specific event occurs. • Event listeners are interfaces and the event-handler is a method declared in the event listener interface that is implemented by the application. • The syntax of a method that registers an event listener with an event source is: public void addTYPEListener(TYPEListener obj) ©NIIT Java Programming Lesson 2B / Slide 9 of 27
Event-handlingEvent-Handling in Java (Contd.) • The Delegation Event Model • The delegation event model is based on the concept that source generates the event and notifies one or more event listeners. • The delegation event model allows you to specify the objects that are to be notified when a specific event occurs. • In delegation event model, the event listener has to be registered with a source in order to get notified about the occurrence of a particular event. ©NIIT Java Programming Lesson 2B / Slide 10 of 27
Event-handlingEvent Classes • The Java event class hierarchy: ©NIIT Java Programming Lesson 2B / Slide 11 of 27
Event-handlingEvent Classes (Contd.)• The Action Event class •ActionEvent is generated by an AWT component, such as a button, when a component-specific action is performed. •The action event is generated when a button is pressed, a list item is double-clicked, or a menu item is selected. •The following syntax shows the declaration of the constructor of the ActionEvent class is: public ActionEvent(Object source, int id, String command) •The main methods included in the Action Event class are: •String getActionCommand() •int getModifiers() ©NIIT Java Programming Lesson 2B / Slide 12 of 27
Event-handlingEvent Classes (Contd.)• The MouseEvent class • The MouseEvent class extends the java.awt.event.InputEvent class. • The mouse event indicates that a mouse action has occurred on a component. • The mouse events include: • Pressing a mouse button • Releasing a mouse button • Clicking a mouse button • Entering of mouse in a component area • Exiting of mouse from a component area • The mouse event class defines some integer constants that can be used to identify several types of mouse events. ©NIIT Java Programming Lesson 2B / Slide 13 of 27
Event-handlingEvent Classes (Contd.)• Various integer constants of the Event class: Constants Description MOUSE_CLICKED Identifies the event of mouse clicking. MOUSE_DRAGGED Identifies the event of dragging of mouse. MOUSE_MOVED Identifies the event of mouse moving. MOUSE_PRESSED Identifies the event of mouse pressing. ©NIIT Java Programming Lesson 2B / Slide 14 of 27
Event-handlingEvent Classes (Contd.) Constants Description MOUSE_RELEASED Identifies the event of mouse releasing. MOUSE_ENTERED Identifies the event of mouse entering an AWT component. MOUSE_EXITED Identifies the event of mouse exiting an AWT component. ©NIIT Java Programming Lesson 2B / Slide 15 of 27
Event-handlingEvent Classes (Contd.) •The following syntax shows the declaration of one of the constructors of the MouseEvent class: public MouseEvent(Component source, int eventType, long when, int modifiers, int x, int y, int clickCount,boolean triggersPopup ) •Various methods of the MouseEvent class: Methods Description public int getX() Returns the horizontal x coordinate of the mouse position relative to a source component. public int getY() Returns the vertical y coordinate of the mouse position relative to a source component. ©NIIT Java Programming Lesson 2B / Slide 16 of 27
Event-handlingEvent Classes (Contd.) Methods Description public point getPoint() Returns the Point object. The Point object contains the x and y coordinates of the mouse position relative to a source component. public void translatePoint(int x, int Translates the coordinates of a mouse y) event to a new position by adding x and y offsets. public int getClickCount() Returns the number of mouse clicks associated with an event. ©NIIT Java Programming Lesson 2B / Slide 17 of 27
Event-handlingEvent Listener Interfaces • An event listener registers with an event source to receive notifications about the events of a particular type. • Various event listener interfaces defined in the java.awt.event package: Interface Description ActionListener Defines the actionPerformed() method to receive and process action events. MouseListener Defines five methods to receive mouse events, such as when a mouse is clicked, pressed, released, enters, or exits a component. MouseMotionListener Defines two methods to receive events, such as when a mouse is dragged or moved. ©NIIT Java Programming Lesson 2B / Slide 18 of 27
Event-handlingEvent Listener Interfaces (Contd.) Interface Description AdjustmentListner Defines the adjustmentValueChanged() method to receive and process the adjustment events. TextListener Defines the textValueChanged() method to receive and process an event when the text value changes. WindowListener Defines seven window methods to receive events. ItemListener Defines the itemStateChanged() method when an item has been selected or deselected by the user. ©NIIT Java Programming Lesson 2B / Slide 19 of 27
Event-handlingDemonstration-Using theActionListener Interface• Problem Statement Create an applet that contains a button. When you click the button, the label of the button is changed from "Click here" to "Button clicked".• Solution • To solve the above problem, perform the following tasks: • Code the application • Compile and execute the application ©NIIT Java Programming Lesson 2B / Slide 20 of 27
Event-handlingUsing the MouseListener Interface • The MouseListener interface is implemented for receiving various mouse events, such as when you press, click, release, enter, and exit a component. • Various public methods declared in the MouseListener interface:Methods Descriptionvoid mouseClicked(MouseEvent me) Performs an action when the mouse button clicks a component.void mousePressed(MouseEvent me) Performs an action when mouse button presses a component.void mouseReleased(MouseEvent me) Performs an action when the mouse button releases a component.void mouseEntered(MouseEvent me) Performs an action when a mouse enters a component area.void mouseExited(MouseEvent me) Performs an action when a mouse exits a component area. ©NIIT Java Programming Lesson 2B / Slide 21 of 27
Event-handlingAdapter Classes • The Java programming language provides adapter classes, which implement the event listener interfaces containing more than one event-handling method. • An adapter class provides an empty implementation of the event-handling methods in an event listener interface. • The adapter classes are useful because they allows you to receive and process only some of the events that are handled by a particular event listener interface. • You can define a class that acts as an event listener by extending one of the adapter classes and overriding its methods to handle a particular type of event. ©NIIT Java Programming Lesson 2B / Slide 22 of 27
Event-handlingAdapter Classes (Contd.) • Various adapter classes: Adapter Class Description KeyAdapter Provides empty implementation of the methods of the KeyListener interface. MouseAdapter Provides empty implementation of the methods of the MouseListener interface. MouseMotionAdapter Provides empty implementation of the methods of the MouseMotionListener interface. ©NIIT Java Programming Lesson 2B / Slide 23 of 27
Event-handlingAdapter Classes (Contd.) • Various adapter classes: (contd.) Adapter Class Description WindowAdapter Provides empty implementation of the methods of the WindowListener and WindowFocusListener interfaces. FocusAdapter Provides empty implementation of the methods of the FocusListener interface. ©NIIT Java Programming Lesson 2B / Slide 24 of 27
Event-handling Adapter Classes (Contd.) • Using the MouseAdapter Class • The MouseAdapter class provides empty implementation of mouse event-handling methods, such as mouseClicked(), mouseEntered(), mouseExited(), mousePressed(), and mouseReleased(). • A class that acts as a listener for mouse events extends the MouseAdapter class and overrides the required methods. •Using the MouseMotionAdapter Class • The MouseMotionAdpater class provides empty implementation of methods, such as mouseDragged() and mouseMoved(). • A class that acts as a listener for mouse motion events extends the MouseAdapter class and overrides the required method. ©NIIT Java Programming Lesson 2B / Slide 25 of 27
Event-handlingSummaryIn this lesson, you learned: • The components of an event-driven program are: • Event Source: Refers to AWT components such as Button, List, Checkbox, and Scrollbar that generate events. • Event Listener- Refers to any object that receives messages or events. • Event Handler- Refers to the method that receives and processes the event. The event handler method takes an Event object as a parameter. • The various event classes in Java, such as the ActionEvent and MouseEvent classes. • To handle action events, you need to register the listener object that implements the ActionListener interface. • To handle mouse events, you need to register the listener object that implements the MouseListener interface. • Events generated by mouse are of two types, mouse events and mouse motion events. ©NIIT Java Programming Lesson 2B / Slide 26 of 27
Event-handlingSummary (Contd.) • Mouse events include, pressing a mouse button, releasing a mouse button, clicking a mouse button, entering of a mouse in a component area, exiting of a mouse from a component area. • Mouse motion events include, moving a mouse and dragging a mouse. • Adapter classes provides an empty implementation of all the methods in an event listener interface. ©NIIT Java Programming Lesson 2B / Slide 27 of 27
Email sent successfully..