SlideShare a Scribd company logo
Java Programming Language
Objectives


                In this session, you will learn to:
                   Define events and event handling
                   Determine the user action that originated the event from the
                   event object details
                   Identify the appropriate listener interface for a variety of event
                   types
                   Create the appropriate event handler methods for a variety of
                   event types
                   Understand the use of inner classes and anonymous classes in
                   event handling
                   Identify the key AWT components and the events they trigger
                   Describe how to create menu, menu bar, menu items and how
                   to control visual aspects




     Ver. 1.0                        Session 11                             Slide 1 of 26
Java Programming Language
Events


                Events: Objects that describe what happened
                Event sources: The generator of an event
                Event handlers: A method that receives an event object,
                deciphers it, and processes the user’s interaction.




     Ver. 1.0                     Session 11                        Slide 2 of 26
Java Programming Language
Delegation Model of Event


                An event can be sent to many event handlers.
                Event handlers register with components when they are
                interested in events generated by that component.




     Ver. 1.0                     Session 11                      Slide 3 of 26
Java Programming Language
Delegation Model of Event (Contd.)


                Client objects (handlers) register with a GUI component that
                they want to observe.
                GUI components only trigger the handlers for the type of
                event that has occurred.
                Most components can trigger more than one type of event.
                The delegation model distributes the work among multiple
                classes.




     Ver. 1.0                      Session 11                        Slide 4 of 26
Java Programming Language
A Listener Example


                This code snippet shows a simple Frame with a single
                button on it, class name is TestButton:
                 public TestButton()
                 {
                   f = new Frame("Test");
                   b = new Button("Press Me!");
                   b.setActionCommand("ButtonPressed");
                 }
                 public void launchFrame()
                 {
                   b.addActionListener(new ButtonHandler());
                   f.add(b,BorderLayout.CENTER);
                   f.pack();
                   f.setVisible(true);
                 }
     Ver. 1.0                    Session 11                    Slide 5 of 26
Java Programming Language
A Listener Example (Contd.)


                Code for the event listener looks like this:
                 import java.awt.event.*;
                 public class ButtonHandler implements
                 ActionListener
                 {
                   public void actionPerformed(ActionEvent e)
                   {
                     System.out.println("Action occurred");
                     System.out.println("Button’s command is:
                     "+ e.getActionCommand());
                   }
                 }
                The event is delegated to ButtonHandler class.

     Ver. 1.0                 Session 11                Slide 6 of 26
Java Programming Language
Demonstration


               Lets see how to use the Event handling API to handle simple
               GUI events.




    Ver. 1.0                        Session 11                       Slide 7 of 26
Java Programming Language
Event Categories


                Class Hierarchy of GUI Events:




     Ver. 1.0                     Session 11     Slide 8 of 26
Java Programming Language
Listener Type


                Some Events and Their Associated Event Listeners:

                 Act that Results in the Event                      Listener Type
                 User clicks a button, presses Enter while typing
                                                                    ActionListener
                 in a text field, or chooses a menu item
                 User closes a frame (main window)                  WindowListener
                 User presses a mouse button while the cursor is
                                                                    MouseListener
                 over a component
                 User moves the mouse over a component              MouseMotionListener
                 Component becomes visible                          ComponentListener

                 Component gets the keyboard focus                  FocusListener




     Ver. 1.0                                  Session 11                                 Slide 9 of 26
Java Programming Language
Listeners


                ActionListener Interface:
                 – Has only one method i.e.
                   actionPerformed(ActionEvent)
                 – To detect when the user clicks an onscreen button (or does the
                   keyboard equivalent), a program must have an object that
                   implements the ActionListener interface.
                 – The program must register this object as an action listener on
                   the button (the event source), using the
                   addActionListener() method.
                 – When the user clicks the onscreen button, the button fires an
                   action event.




     Ver. 1.0                       Session 11                          Slide 10 of 26
Java Programming Language
Listeners (Contd.)


                MouseListener interface:
                – To detect the mouse clicking, a program must have an object
                  that implements the MouseListener interface.
                – This interface includes several events including
                  mouseEntered, mouseExited, mousePressed,
                  mouseReleased, and mouseClicked.
                – When the user clicks the onscreen button, the button fires an
                  action event.




     Ver. 1.0                       Session 11                          Slide 11 of 26
Java Programming Language
Listeners (Contd.)


                Implementing Multiple Interfaces:
                   A class can be declared with Multiple Interfaces by using
                   comma separation:
                    • Implements MouseListener,MouseMotionListener
                Listening to Multiple Sources:
                   Multiple listeners cause unrelated parts of a program to react
                   to the same event.
                   The handlers of all registered listeners are called when the
                   event occurs.




     Ver. 1.0                       Session 11                            Slide 12 of 26
Java Programming Language
Event Adapters


                The listener classes that you define can extend adapter
                classes and override only the methods that you need.
                An example is:
                 import java.awt.*;
                 import java.awt.event.*;
                 public class MouseClickHandler extends
                 MouseAdapter
                 {
                   //We just need the mouseClick handler,
                   so //we use an adapter to avoid having
                   to //write all the event handler methods




     Ver. 1.0                     Session 11                      Slide 13 of 26
Java Programming Language
Event Adapters (Contd.)


                    public void mouseClicked(MouseEvent e)
                    {
                    // Do stuff with the mouse click...
                    }
                }




     Ver. 1.0                   Session 11              Slide 14 of 26
Java Programming Language
Inner Classes


                Event Handling Using Inner Classes:
                   Using inner classes for event handles gives access to the
                   private data of the outer class.




     Ver. 1.0                       Session 11                           Slide 15 of 26
Java Programming Language
MenuBar


               Frames can contain a menu bar, a menu bar can contain
               zero or more menus, and menu can contain zero or more
               menu items (including submenus).
               Let’s see how to do this.




    Ver. 1.0                    Session 11                     Slide 16 of 26
Java Programming Language
Creating a MenuBar


                Create a MenuBar object, and set it into a menu container,
                such as a Frame. For example:
                 Frame f = new Frame("MenuBar");
                 MenuBar mb = new MenuBar();
                 f.setMenuBar(mb);




     Ver. 1.0                      Session 11                       Slide 17 of 26
Java Programming Language
Creating a Menu


                Create one or more Menu objects, and add them to the
                menu bar object. For example:
                 Frame f = new Frame("Menu");
                 MenuBar mb = new MenuBar();
                 Menu m1 = new Menu("File");
                 Menu m2 = new Menu("Edit");
                 Menu m3 = new Menu("Help");
                 mb.add(m1);
                 mb.add(m2);
                 mb.setHelpMenu(m3);
                 f.setMenuBar(mb);



     Ver. 1.0                     Session 11                     Slide 18 of 26
Java Programming Language
Creating a MenuItem


               Create one or more MenuItem objects, and add them to the
               menu object. For example:
                MenuItem mi1 = new MenuItem("New");
                MenuItem mi2 = new MenuItem("Save");
                MenuItem mi3 = new MenuItem("Load");
                MenuItem mi4 = new MenuItem("Quit");
                mi1.addActionListener(this);
                mi2.addActionListener(this);
                mi3.addActionListener(this);
                mi4.addActionListener(this);
                m1.add(mi1);
                m1.add(mi2);


    Ver. 1.0                     Session 11                     Slide 19 of 26
Java Programming Language
Creating a MenuItem (Contd.)


                 m1.add(mi3);
                 m1.addSeparator();
                 m1.add(mi4);
                Let’ see how MenuItem will look like.




     Ver. 1.0                      Session 11           Slide 20 of 26
Java Programming Language
Demonstration


               Lets see how to add a menu and other GUI components to a
               AWT application.




    Ver. 1.0                       Session 11                     Slide 21 of 26
Java Programming Language
Creating a CheckBoxMenuItem


               Creating a CheckBoxMenuItem:
                CheckboxMenuItem mi5 =
                newCheckboxMenuItem("Persistent");
                mi5.addItemListener(this);
                m1.add(mi5);




    Ver. 1.0                 Session 11              Slide 22 of 26
Java Programming Language
Controlling Visual Aspects


                Commands to control visual aspects of the GUI include:
                   Colors:
                    setForeground()
                    setBackground()
                    Example:
                    Color purple = new Color(255, 0, 255);
                    Button b = new Button(“Purple”);
                    b.setBackground(purple);




     Ver. 1.0                     Session 11                       Slide 23 of 26
Java Programming Language
J.F.C./Swing Technology


                •   Java Foundation Class/Swing (J.F.C./Swing) technology is
                    a second-generation GUI toolkit.
                •   It builds on top of AWT, but supplants the components with
                    lightweight versions.
                •   There are many more components, and much more
                    complex components, including JTable, JTree, and
                    JComboBox.




     Ver. 1.0                          Session 11                      Slide 24 of 26
Java Programming Language
Summary


               In this session, you learned that:
                  When user perform some action, for example, button click or
                  mouse move then the program performs some action which is
                  called event.
                  Events can be handled by implementing appropriate Listener
                  Interface.
                  Most components can trigger more than one type of event.
                  The delegation model distributes the work among multiple
                  classes.
                  ActionListener Interface:
                    • When the user clicks an onscreen button (or does the keyboard
                      equivalent), a program must have an object that implements the
                      ActionListener interface.
                  MouseListener Interface:
                    • To detect the mouse clicking, a program must have an object that
                      implements the MouseListener interface.




    Ver. 1.0                         Session 11                              Slide 25 of 26
Java Programming Language
Summary (Contd.)


               – A class can be declared with multiple interfaces by using
                 comma separation.
               – Event Adapter classes can be used in place of implementing
                 listener, if you need to implement only one method.
               – Manubar can be created by creating a MenuBar class object,
                 and set it into a menu container, such as a Frame.
               – Menu class object is used to create menu, and add them to the
                 MenuBar object.
               – MenuItems can be created by creating one or more MenuItem
                 class objects, and add them to the menu object.
               – Checked menuitems can be created by using
                 CheckboxMenuItem class object.
               – Colors can be set by creating the Color class object.



    Ver. 1.0                      Session 11                          Slide 26 of 26

More Related Content

What's hot

13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19
Niit Care
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
Java Lover
 
Java swing 1
Java swing 1Java swing 1
Java swing 1
Mukesh Tekwani
 
03 iec t1_s1_plt_session_03
03 iec t1_s1_plt_session_0303 iec t1_s1_plt_session_03
03 iec t1_s1_plt_session_03
Niit Care
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
jyoti_lakhani
 
1
11
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
Ideal Eyes Business College
 
Proyect of english
Proyect of englishProyect of english
Proyect of english
Carlos Alcivar
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
Ravi Kant Sahu
 
Introduction to Java Programming
Introduction to Java Programming Introduction to Java Programming
Introduction to Java Programming
Saravanakumar R
 
12slide
12slide12slide
JavaYDL12
JavaYDL12JavaYDL12
JavaYDL12
Terry Yoast
 
Java Reference
Java ReferenceJava Reference
Java Reference
khoj4u
 
09 gui 13
09 gui 1309 gui 13
09 gui 13
Niit Care
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
Vibrant Technologies & Computers
 
09 intel v_tune_session_13
09 intel v_tune_session_1309 intel v_tune_session_13
09 intel v_tune_session_13
Niit Care
 
Ex11 mini project
Ex11 mini projectEx11 mini project
Ex11 mini project
Elanthendral Mariappan
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
KUNAL GADHIA
 
Visual Studio Automation Object Model. EnvDTE interfaces
Visual Studio Automation Object Model. EnvDTE interfacesVisual Studio Automation Object Model. EnvDTE interfaces
Visual Studio Automation Object Model. EnvDTE interfaces
PVS-Studio
 

What's hot (20)

 
13 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_1913 iec t1_s1_oo_ps_session_19
13 iec t1_s1_oo_ps_session_19
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Java swing 1
Java swing 1Java swing 1
Java swing 1
 
03 iec t1_s1_plt_session_03
03 iec t1_s1_plt_session_0303 iec t1_s1_plt_session_03
03 iec t1_s1_plt_session_03
 
1 java programming- introduction
1  java programming- introduction1  java programming- introduction
1 java programming- introduction
 
1
11
1
 
Java basic introduction
Java basic introductionJava basic introduction
Java basic introduction
 
Proyect of english
Proyect of englishProyect of english
Proyect of english
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Introduction to Java Programming
Introduction to Java Programming Introduction to Java Programming
Introduction to Java Programming
 
12slide
12slide12slide
12slide
 
JavaYDL12
JavaYDL12JavaYDL12
JavaYDL12
 
Java Reference
Java ReferenceJava Reference
Java Reference
 
09 gui 13
09 gui 1309 gui 13
09 gui 13
 
Professional-core-java-training
Professional-core-java-trainingProfessional-core-java-training
Professional-core-java-training
 
09 intel v_tune_session_13
09 intel v_tune_session_1309 intel v_tune_session_13
09 intel v_tune_session_13
 
Ex11 mini project
Ex11 mini projectEx11 mini project
Ex11 mini project
 
Fundamentals of JAVA
Fundamentals of JAVAFundamentals of JAVA
Fundamentals of JAVA
 
Visual Studio Automation Object Model. EnvDTE interfaces
Visual Studio Automation Object Model. EnvDTE interfacesVisual Studio Automation Object Model. EnvDTE interfaces
Visual Studio Automation Object Model. EnvDTE interfaces
 

Viewers also liked

Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
Java Programming
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Java session05
Java session05Java session05
Java session05
Niit Care
 
Jdbc session02
Jdbc session02Jdbc session02
Jdbc session02
Niit Care
 
Dacj 2-2 b
Dacj 2-2 bDacj 2-2 b
Dacj 2-2 b
Niit Care
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
teach4uin
 
Java session02
Java session02Java session02
Java session02
Niit Care
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event Handling
Shraddha
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
arnold 7490
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
Ayesha Kanwal
 
Event handling
Event handlingEvent handling
Training on java niit (sahil gupta 9068557926)
Training on java niit (sahil gupta 9068557926)Training on java niit (sahil gupta 9068557926)
Training on java niit (sahil gupta 9068557926)
Sahil Gupta
 
Java session08
Java session08Java session08
Java session08
Niit Care
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
Niit Care
 
Java awt
Java awtJava awt
Java awt
Arati Gadgil
 
Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
Niit Care
 

Viewers also liked (16)

Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Java session05
Java session05Java session05
Java session05
 
Jdbc session02
Jdbc session02Jdbc session02
Jdbc session02
 
Dacj 2-2 b
Dacj 2-2 bDacj 2-2 b
Dacj 2-2 b
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Java session02
Java session02Java session02
Java session02
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event Handling
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Event handling
Event handlingEvent handling
Event handling
 
Training on java niit (sahil gupta 9068557926)
Training on java niit (sahil gupta 9068557926)Training on java niit (sahil gupta 9068557926)
Training on java niit (sahil gupta 9068557926)
 
Java session08
Java session08Java session08
Java session08
 
Dacj 1-1 b
Dacj 1-1 bDacj 1-1 b
Dacj 1-1 b
 
Java awt
Java awtJava awt
Java awt
 
Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 

Similar to Java session11

Lecture 18
Lecture 18Lecture 18
Lecture 18
Debasish Pratihari
 
Java gui event
Java gui eventJava gui event
Java gui event
SoftNutx
 
Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
Satish Verma
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
Chapter11 graphical components
Chapter11 graphical componentsChapter11 graphical components
Chapter11 graphical components
Arifa Fatima
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
SURBHI SAROHA
 
Event handling
Event handlingEvent handling
Event handling
swapnac12
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
usama537223
 
ACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptx
MattFlordeliza1
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
pratikkadam78
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
What is Event
What is EventWhat is Event
What is Event
Asmita Prasad
 
Java
JavaJava
AWT
AWT AWT
09events
09events09events
09events
Waheed Warraich
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
Dhairya Joshi
 

Similar to Java session11 (20)

Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Chapter11 graphical components
Chapter11 graphical componentsChapter11 graphical components
Chapter11 graphical components
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
Event handling
Event handlingEvent handling
Event handling
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
ACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptx
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
What is Event
What is EventWhat is Event
What is Event
 
Java
JavaJava
Java
 
AWT
AWT AWT
AWT
 
09events
09events09events
09events
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
 

More from Niit Care

Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
Niit Care
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
Niit Care
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
Niit Care
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
Niit Care
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
Niit Care
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
Niit Care
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
Niit Care
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
Niit Care
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
Niit Care
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
Niit Care
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
Niit Care
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
Niit Care
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
Niit Care
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
Niit Care
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
Niit Care
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
Niit Care
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
Niit Care
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
Niit Care
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
Niit Care
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
Niit Care
 

More from Niit Care (20)

Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 
Dacj 1-3 b
Dacj 1-3 bDacj 1-3 b
Dacj 1-3 b
 

Recently uploaded

PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
christinelarrosa
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
Vadym Kazulkin
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
What is an RPA CoE? Session 2 – CoE Roles
What is an RPA CoE?  Session 2 – CoE RolesWhat is an RPA CoE?  Session 2 – CoE Roles
What is an RPA CoE? Session 2 – CoE Roles
DianaGray10
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
LizaNolte
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
FilipTomaszewski5
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
DanBrown980551
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
Enterprise Knowledge
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 

Recently uploaded (20)

PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
What is an RPA CoE? Session 2 – CoE Roles
What is an RPA CoE?  Session 2 – CoE RolesWhat is an RPA CoE?  Session 2 – CoE Roles
What is an RPA CoE? Session 2 – CoE Roles
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeckPoznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
Poznań ACE event - 19.06.2024 Team 24 Wrapup slidedeck
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Demystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through StorytellingDemystifying Knowledge Management through Storytelling
Demystifying Knowledge Management through Storytelling
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 

Java session11

  • 1. Java Programming Language Objectives In this session, you will learn to: Define events and event handling Determine the user action that originated the event from the event object details Identify the appropriate listener interface for a variety of event types Create the appropriate event handler methods for a variety of event types Understand the use of inner classes and anonymous classes in event handling Identify the key AWT components and the events they trigger Describe how to create menu, menu bar, menu items and how to control visual aspects Ver. 1.0 Session 11 Slide 1 of 26
  • 2. Java Programming Language Events Events: Objects that describe what happened Event sources: The generator of an event Event handlers: A method that receives an event object, deciphers it, and processes the user’s interaction. Ver. 1.0 Session 11 Slide 2 of 26
  • 3. Java Programming Language Delegation Model of Event An event can be sent to many event handlers. Event handlers register with components when they are interested in events generated by that component. Ver. 1.0 Session 11 Slide 3 of 26
  • 4. Java Programming Language Delegation Model of Event (Contd.) Client objects (handlers) register with a GUI component that they want to observe. GUI components only trigger the handlers for the type of event that has occurred. Most components can trigger more than one type of event. The delegation model distributes the work among multiple classes. Ver. 1.0 Session 11 Slide 4 of 26
  • 5. Java Programming Language A Listener Example This code snippet shows a simple Frame with a single button on it, class name is TestButton: public TestButton() { f = new Frame("Test"); b = new Button("Press Me!"); b.setActionCommand("ButtonPressed"); } public void launchFrame() { b.addActionListener(new ButtonHandler()); f.add(b,BorderLayout.CENTER); f.pack(); f.setVisible(true); } Ver. 1.0 Session 11 Slide 5 of 26
  • 6. Java Programming Language A Listener Example (Contd.) Code for the event listener looks like this: import java.awt.event.*; public class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("Action occurred"); System.out.println("Button’s command is: "+ e.getActionCommand()); } } The event is delegated to ButtonHandler class. Ver. 1.0 Session 11 Slide 6 of 26
  • 7. Java Programming Language Demonstration Lets see how to use the Event handling API to handle simple GUI events. Ver. 1.0 Session 11 Slide 7 of 26
  • 8. Java Programming Language Event Categories Class Hierarchy of GUI Events: Ver. 1.0 Session 11 Slide 8 of 26
  • 9. Java Programming Language Listener Type Some Events and Their Associated Event Listeners: Act that Results in the Event Listener Type User clicks a button, presses Enter while typing ActionListener in a text field, or chooses a menu item User closes a frame (main window) WindowListener User presses a mouse button while the cursor is MouseListener over a component User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Ver. 1.0 Session 11 Slide 9 of 26
  • 10. Java Programming Language Listeners ActionListener Interface: – Has only one method i.e. actionPerformed(ActionEvent) – To detect when the user clicks an onscreen button (or does the keyboard equivalent), a program must have an object that implements the ActionListener interface. – The program must register this object as an action listener on the button (the event source), using the addActionListener() method. – When the user clicks the onscreen button, the button fires an action event. Ver. 1.0 Session 11 Slide 10 of 26
  • 11. Java Programming Language Listeners (Contd.) MouseListener interface: – To detect the mouse clicking, a program must have an object that implements the MouseListener interface. – This interface includes several events including mouseEntered, mouseExited, mousePressed, mouseReleased, and mouseClicked. – When the user clicks the onscreen button, the button fires an action event. Ver. 1.0 Session 11 Slide 11 of 26
  • 12. Java Programming Language Listeners (Contd.) Implementing Multiple Interfaces: A class can be declared with Multiple Interfaces by using comma separation: • Implements MouseListener,MouseMotionListener Listening to Multiple Sources: Multiple listeners cause unrelated parts of a program to react to the same event. The handlers of all registered listeners are called when the event occurs. Ver. 1.0 Session 11 Slide 12 of 26
  • 13. Java Programming Language Event Adapters The listener classes that you define can extend adapter classes and override only the methods that you need. An example is: import java.awt.*; import java.awt.event.*; public class MouseClickHandler extends MouseAdapter { //We just need the mouseClick handler, so //we use an adapter to avoid having to //write all the event handler methods Ver. 1.0 Session 11 Slide 13 of 26
  • 14. Java Programming Language Event Adapters (Contd.) public void mouseClicked(MouseEvent e) { // Do stuff with the mouse click... } } Ver. 1.0 Session 11 Slide 14 of 26
  • 15. Java Programming Language Inner Classes Event Handling Using Inner Classes: Using inner classes for event handles gives access to the private data of the outer class. Ver. 1.0 Session 11 Slide 15 of 26
  • 16. Java Programming Language MenuBar Frames can contain a menu bar, a menu bar can contain zero or more menus, and menu can contain zero or more menu items (including submenus). Let’s see how to do this. Ver. 1.0 Session 11 Slide 16 of 26
  • 17. Java Programming Language Creating a MenuBar Create a MenuBar object, and set it into a menu container, such as a Frame. For example: Frame f = new Frame("MenuBar"); MenuBar mb = new MenuBar(); f.setMenuBar(mb); Ver. 1.0 Session 11 Slide 17 of 26
  • 18. Java Programming Language Creating a Menu Create one or more Menu objects, and add them to the menu bar object. For example: Frame f = new Frame("Menu"); MenuBar mb = new MenuBar(); Menu m1 = new Menu("File"); Menu m2 = new Menu("Edit"); Menu m3 = new Menu("Help"); mb.add(m1); mb.add(m2); mb.setHelpMenu(m3); f.setMenuBar(mb); Ver. 1.0 Session 11 Slide 18 of 26
  • 19. Java Programming Language Creating a MenuItem Create one or more MenuItem objects, and add them to the menu object. For example: MenuItem mi1 = new MenuItem("New"); MenuItem mi2 = new MenuItem("Save"); MenuItem mi3 = new MenuItem("Load"); MenuItem mi4 = new MenuItem("Quit"); mi1.addActionListener(this); mi2.addActionListener(this); mi3.addActionListener(this); mi4.addActionListener(this); m1.add(mi1); m1.add(mi2); Ver. 1.0 Session 11 Slide 19 of 26
  • 20. Java Programming Language Creating a MenuItem (Contd.) m1.add(mi3); m1.addSeparator(); m1.add(mi4); Let’ see how MenuItem will look like. Ver. 1.0 Session 11 Slide 20 of 26
  • 21. Java Programming Language Demonstration Lets see how to add a menu and other GUI components to a AWT application. Ver. 1.0 Session 11 Slide 21 of 26
  • 22. Java Programming Language Creating a CheckBoxMenuItem Creating a CheckBoxMenuItem: CheckboxMenuItem mi5 = newCheckboxMenuItem("Persistent"); mi5.addItemListener(this); m1.add(mi5); Ver. 1.0 Session 11 Slide 22 of 26
  • 23. Java Programming Language Controlling Visual Aspects Commands to control visual aspects of the GUI include: Colors: setForeground() setBackground() Example: Color purple = new Color(255, 0, 255); Button b = new Button(“Purple”); b.setBackground(purple); Ver. 1.0 Session 11 Slide 23 of 26
  • 24. Java Programming Language J.F.C./Swing Technology • Java Foundation Class/Swing (J.F.C./Swing) technology is a second-generation GUI toolkit. • It builds on top of AWT, but supplants the components with lightweight versions. • There are many more components, and much more complex components, including JTable, JTree, and JComboBox. Ver. 1.0 Session 11 Slide 24 of 26
  • 25. Java Programming Language Summary In this session, you learned that: When user perform some action, for example, button click or mouse move then the program performs some action which is called event. Events can be handled by implementing appropriate Listener Interface. Most components can trigger more than one type of event. The delegation model distributes the work among multiple classes. ActionListener Interface: • When the user clicks an onscreen button (or does the keyboard equivalent), a program must have an object that implements the ActionListener interface. MouseListener Interface: • To detect the mouse clicking, a program must have an object that implements the MouseListener interface. Ver. 1.0 Session 11 Slide 25 of 26
  • 26. Java Programming Language Summary (Contd.) – A class can be declared with multiple interfaces by using comma separation. – Event Adapter classes can be used in place of implementing listener, if you need to implement only one method. – Manubar can be created by creating a MenuBar class object, and set it into a menu container, such as a Frame. – Menu class object is used to create menu, and add them to the MenuBar object. – MenuItems can be created by creating one or more MenuItem class objects, and add them to the menu object. – Checked menuitems can be created by using CheckboxMenuItem class object. – Colors can be set by creating the Color class object. Ver. 1.0 Session 11 Slide 26 of 26