SlideShare ist ein Scribd-Unternehmen logo
1 von 26
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (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
 

Andere mochten auch

Andere mochten auch (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
 

Ähnlich wie Java session11

Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
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. 2024nehakumari0xf
 
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. 2024kashyapneha2809
 
Chapter11 graphical components
Chapter11 graphical componentsChapter11 graphical components
Chapter11 graphical componentsArifa Fatima
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingPayal Dungarwal
 
Event handling
Event handlingEvent handling
Event handlingswapnac12
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
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.pptxMattFlordeliza1
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FXpratikkadam78
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxudithaisur
 

Ähnlich wie 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
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
 
JavaYDL16
JavaYDL16JavaYDL16
JavaYDL16
 

Mehr von 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
 

Kürzlich hochgeladen

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 

Kürzlich hochgeladen (20)

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 

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