SlideShare a Scribd company logo
JAVA (UNIT 5)
BY:SURBHI SAROHA
SYLLABUS
 Event Handling: Different Mechanism
 The delegation Event Model
 Event Classes
 Listener Interfaces
 Adapter and Inner Classes
 Working with windows
 Graphics and text
 Using AWT controls
 Layout managers and menus
 Java Applet
 BEANS: Introduction to java Beans and Swings
 Servlets
Event Handling: Different Mechanism
 An event can be defined as changing the state of an object or behavior by performing actions.
Actions can be a button click, cursor movement, keypress through keyboard or page scrolling, etc.
 The java.awt.event package can be used to provide various event classes.
 Classification of Events
 Foreground Events
 Background Events
Cont…..
 1. Foreground Events
 Foreground events are the events that require user interaction to generate, i.e., foreground events
are generated due to interaction by the user on components in Graphic User Interface (GUI).
Interactions are nothing but clicking on a button, scrolling the scroll bar, cursor moments, etc.
 2. Background Events
 Events that don’t require interactions of users to generate are known as background events.
Examples of these events are operating system failures/interrupts, operation completion, etc.
Event Handling
 It is a mechanism to control the events and to decide what should happen after an event occur. To
handle the events, Java follows the Delegation Event model.
 Delegation Event model
 It has Sources and Listeners.
 Source: Events are generated from the source. There are various sources like buttons, checkboxes,
list, menu-item, choice, scrollbar, text components, windows, etc., to generate events.
 Listeners: Listeners are used for handling the events generated from the source. Each of these
listeners represents interfaces that are responsible for handling events.
 To perform Event Handling, we need to register the source with the listener.
The delegation Event Model
 The Delegation Event model is defined to handle events in GUI programming languages.
The GUI stands for Graphical User Interface, where a user graphically/visually interacts with the
system.
 The GUI programming is inherently event-driven; whenever a user initiates an activity such as a
mouse activity, clicks, scrolling, etc., each is known as an event that is mapped to a code to respond
to functionality to the user. This is known as event handling.
 In this section, we will discuss event processing and how to implement the delegation event model
in Java. We will also discuss the different components of an Event Model.
Event Classes
 Changing the state of an object is known as an event. For example, click on button, dragging mouse
etc.
 The java.awt.event package provides many event classes and Listener interfaces for event handling.
Java Event classes and Listener interfaces
Adapter and Inner Classes
 Java adapter classes provide the default implementation of listener interfaces.
 If you inherit the adapter class, you will not be forced to provide the implementation of all the
methods of listener interfaces.
 So it saves code.
Pros of using Adapter classes:
 It assists the unrelated classes to work combinedly.
 It provides ways to use classes in different ways.
 It increases the transparency of classes.
 It provides a way to include related patterns in the class.
 It provides a pluggable kit for developing an application.
 It increases the reusability of the class.
java.awt.event Adapter classes
 The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages.
Inner Classes
 In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group
classes that belong together, which makes your code more readable and maintainable.
 To access the inner class, create an object of the outer class, and then create an object of the inner class:
 class OuterClass {
 int x = 10;
 class InnerClass {
 int y = 5;
 }
 }
Cont…..
 public class Main {
 public static void main(String[] args) {
 OuterClass myOuter = new OuterClass();
 OuterClass.InnerClass myInner = myOuter.new InnerClass();
 System.out.println(myInner.y + myOuter.x);
 }
 }
Working with windows
 What is Windows Programming
 Although the answer to this question always seems improper, Windows programming is the type of
programming that commonly relates to performing programming on the Windows OS platform.
 Thus a Windows program is that program that gets executed on Windows.
 Windows Programming using Java relates to the Java programming on Graphical User Interface and
provides a user-friendly environment for the user.
 A user finds to work with Java more friendly and in a great way.
 In simple words using and executing Java programs on Windows OS defines Windows
programming.
Windows Programming with Java
 Windows Programming with Java is the utilization of the CPU, its registers, and other hardware and
software devices to use and execute the task.
 In Java, it has libraries that support the Windows OS.
 Java Windows Programming Technique
Java APIs
 Currently, there exist the following three sets of Java APIs for performing graphics programming:
 1) Abstract Windowing Toolkit (AWT): The concept of such API came into existence in JDK 1.0, in
which several AWT components become obsolete as well as should be replaced by newer Swing
components.
 2) Swing API: A more comprehensive set of graphics libraries that enhances the AWT. This API was
introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. After the
introduction of JDK 1.1, Swing API came as a part of JFC (Java Foundation Classes). It consists of
Java2D, Accessibility, Swings, Internationalization, and Pluggable Look-and-Feel Support APIs. From
JDK 1.2, JFC has been integrated into core Java.
 3) Tool/IDE for executing Java code: Need to have Notepad++, Eclipse, IntelliJ, or Netbeans IDEs
that allow a user to write and execute programs.
Graphics and text
 Graphics is an abstract class provided by Java AWT which is used to draw or paint on the
components.
 It consists of various fields which hold information like components to be painted, font, color, XOR
mode, etc., and methods that allow drawing various shapes on the GUI components.
 Graphics is an abstract class and thus cannot be initialized directly.
 Objects of its child classes can be obtained in the following two ways.
Cont….
 1. Inside paint() or update() method
 It is passed as an argument to paint and update methods and therefore can be accessed inside
these methods. paint() and update() methods are present in the Component class and thus can be
overridden for the component to be painted.
 void paint(Graphics g)
 void update(Graphics g)
Cont….
 import java.awt.*;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;

 public class MyFrame extends Frame {
 public MyFrame()
 {
 setVisible(true);
 setSize(300, 200);

Cont…..
 addWindowListener(new WindowAdapter() {
 @Override
 public void windowClosing(WindowEvent e)
 {
 System.exit(0);
 }
 });
Cont….
 }
 public void paint(Graphics g)
 {
 g.drawRect(100, 100, 100, 50);
 }

 public static void main(String[] args)
 {
 new MyFrame();
 }
 }
Cont….
 2. getGraphics() method
 This method is present in the Component class and thus can be called on any Component in order to get
the Graphics object for the component.
 import java.awt.*;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;

 public class MyFrame extends Frame {
 public MyFrame()
 {
 setVisible(true);
Cont….
 setSize(300, 200);
 addWindowListener(new WindowAdapter() {
 @Override
 public void windowClosing(WindowEvent e)
 {
 System.exit(0);
 }
 });
 }
Cont….
 public void paint(Graphics g)
 {
 System.out.println("painting...");
 }

 public static void main(String[] args)
 {
 MyFrame f = new MyFrame();
 Graphics g = f.getGraphics();
 System.out.println("drawing...");
 g.drawRect(100, 100, 10, 10);
 System.out.println("drawn...");
 }
 }
Using AWT controls
 The AWT supports the following types of
 controls:
 • Labels
 • Push buttons
 • Check boxes
 • Choice lists
 • Lists
 • Scroll bars
 • Text Editing
 These controls are subclasses of Component.
Layout managers and menus
 The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components in GUI forms.
LayoutManager is an interface that is implemented by all the classes of layout managers. There are
the following classes that represent the layout managers:
 java.awt.BorderLayout
 java.awt.FlowLayout
 java.awt.GridLayout
 java.awt.CardLayout
Cont…..
 java.awt.GridBagLayout
 javax.swing.BoxLayout
 javax.swing.GroupLayout
 javax.swing.ScrollPaneLayout
 javax.swing.SpringLayout etc.
menus
 The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the
MenuItem or any of its subclass.
 The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.
 import java.awt.*;
 class MenuExample
 {
 MenuExample(){
 Frame f= new Frame("Menu and MenuItem Example");
 MenuBar mb=new MenuBar();
 Menu menu=new Menu("Menu");
 Menu submenu=new Menu("Sub Menu");
 MenuItem i1=new MenuItem("Item 1");
Cont….
 MenuItem i2=new MenuItem("Item 2");
 MenuItem i3=new MenuItem("Item 3");
 MenuItem i4=new MenuItem("Item 4");
 MenuItem i5=new MenuItem("Item 5");
 menu.add(i1);
 menu.add(i2);
 menu.add(i3);
 submenu.add(i4);
 submenu.add(i5);
 menu.add(submenu);
 mb.add(menu);
 f.setMenuBar(mb);
Cont…..
 f.setSize(400,400);
 f.setLayout(null);
 f.setVisible(true);
 }
 public static void main(String args[])
 {
 new MenuExample();
 }
 }
Java Applet
 An applet is a Java program that runs in a Web browser.
 An applet can be a fully functional Java application because it has the entire Java API at its disposal.
 An applet is a Java class that extends the java.applet.Applet class.
 A main() method is not invoked on an applet, and an applet class will not define main().
 Applets are designed to be embedded within an HTML page.
 When a user views an HTML page that contains an applet, the code for the applet is downloaded to the
user's machine.
 A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate
runtime environment.
 The JVM on the user's machine creates an instance of the applet class and invokes various methods
during the applet's lifetime.
CONT….
 Applets have strict security rules that are enforced by the Web browser. The security of an applet is
often referred to as sandbox security, comparing the applet to a child playing in a sandbox with
various rules that must be followed.
 Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
Life Cycle of an Applet
 Four methods in the Applet class gives you the framework on which you build any serious applet −
 init − This method is iintended for whatever initialization is needed for your applet. It is called after
the param tags inside the applet tag have been processed.
 start − This method is automatically called after the browser calls the init method. It is also called
whenever the user returns to the page containing the applet after having gone off to other pages.
 stop − This method is automatically called when the user moves off the page on which the applet
sits. It can, therefore, be called repeatedly in the same applet.
CONT…..
 destroy − This method is only called when the browser shuts down normally. Because applets are
meant to live on an HTML page, you should not normally leave resources behind after a user leaves
the page that contains the applet.
 paint − Invoked immediately after the start() method, and also any time the applet needs to repaint
itself in the browser. The paint() method is actually inherited from the java.awt.
A "Hello, World" Applet
 import java.applet.*;
 import java.awt.*;
 public class HelloWorldApplet extends Applet {
 public void paint (Graphics g) {
 g.drawString ("Hello World", 25, 50);
 }
 }
BEANS: Introduction to java Beans and Swings
 A JavaBean is a Java class that should follow the following conventions:
 It should have a no-arg constructor.
 It should be Serializable.
 It should provide methods to set and get the values of the properties, known as getter and setter
methods.
Why use JavaBean?
 According to Java white paper, it is a reusable software component.
 A bean encapsulates many objects into one object so that we can access this object from multiple
places.
 Moreover, it provides easy maintenance.
Simple example of JavaBean class
 //Employee.java

 package mypack;
 public class Employee implements java.io.Serializable{
 private int id;
 private String name;
 public Employee(){}
Cont……
 public void setId(int id){this.id=id;}
 public int getId(){return id;}
 public void setName(String name){this.name=name;}
 public String getName(){return name;}
 }
Swings
 Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based
applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in
java.
 Unlike AWT, Java Swing provides platform-independent and lightweight components.
 The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea,
JRadioButton, JCheckbox, JMenu, JColorChooser etc.
 Java swing components are platform-independent.
 Swing components are lightweight.
 Swing supports pluggable look and feel.
The hierarchy of java swing API is given below
File: FirstSwingExample.java
 import javax.swing.*;
 public class FirstSwingExample {
 public static void main(String[] args) {
 JFrame f=new JFrame();//creating instance of JFrame
 JButton b=new JButton("click");//creating instance of JButton
 b.setBounds(130,100,100, 40);//x axis, y axis, width, height
 f.add(b);//adding button in JFrame
 f.setSize(400,500);//400 width and 500 height
 f.setLayout(null);//using no layout managers
 f.setVisible(true);//making the frame visible
 }
 }
Servlets
 Servlets are the Java programs that run on the Java-enabled web server or application server.
 They are used to handle the request obtained from the webserver, process the request, produce
the response, then send a response back to the webserver.
 Properties of Servlets are as follows:
 Servlets work on the server-side.
 Servlets are capable of handling complex requests obtained from the webserver.
Servlet Architecture is can be depicted from the image itself as
provided below as follows:
Execution of Servlets basically involves six
basic steps:
 The clients send the request to the webserver.
 The web server receives the request.
 The web server passes the request to the corresponding servlet.
 The servlet processes the request and generates the response in the form of output.
 The servlet sends the response back to the webserver.
 The web server sends the response back to the client and the client browser displays it on the
screen.
Thank you 

More Related Content

Similar to JAVA (UNIT 5)

Slot04 creating gui
Slot04 creating guiSlot04 creating gui
Slot04 creating gui
Viên Mai
 
Applications use in Java GUIThe Java GUI consists of a separate, .pdf
Applications use in Java GUIThe Java GUI consists of a separate, .pdfApplications use in Java GUIThe Java GUI consists of a separate, .pdf
Applications use in Java GUIThe Java GUI consists of a separate, .pdf
akshay1213
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
sotlsoc
 

Similar to JAVA (UNIT 5) (20)

Slot04 creating gui
Slot04 creating guiSlot04 creating gui
Slot04 creating gui
 
The Use of Java Swing’s Components to Develop a Widget
The Use of Java Swing’s Components to Develop a WidgetThe Use of Java Swing’s Components to Develop a Widget
The Use of Java Swing’s Components to Develop a Widget
 
GUI design using JAVAFX.ppt
GUI design using JAVAFX.pptGUI design using JAVAFX.ppt
GUI design using JAVAFX.ppt
 
Java swing 1
Java swing 1Java swing 1
Java swing 1
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
Chapter 1-Note.docx
Chapter 1-Note.docxChapter 1-Note.docx
Chapter 1-Note.docx
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
Applications use in Java GUIThe Java GUI consists of a separate, .pdf
Applications use in Java GUIThe Java GUI consists of a separate, .pdfApplications use in Java GUIThe Java GUI consists of a separate, .pdf
Applications use in Java GUIThe Java GUI consists of a separate, .pdf
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdfUNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
 
JavaFX ALA ppt.pptx
JavaFX ALA ppt.pptxJavaFX ALA ppt.pptx
JavaFX ALA ppt.pptx
 
GUI.pptx
GUI.pptxGUI.pptx
GUI.pptx
 
Applet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java AppletsApplet Architecture - Introducing Java Applets
Applet Architecture - Introducing Java Applets
 
GUI.pdf
GUI.pdfGUI.pdf
GUI.pdf
 
GUI (graphical user interface)
GUI (graphical user interface)GUI (graphical user interface)
GUI (graphical user interface)
 
Java session13
Java session13Java session13
Java session13
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 

More from SURBHI SAROHA

More from SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 

Recently uploaded

Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 

JAVA (UNIT 5)

  • 2. SYLLABUS  Event Handling: Different Mechanism  The delegation Event Model  Event Classes  Listener Interfaces  Adapter and Inner Classes  Working with windows  Graphics and text  Using AWT controls  Layout managers and menus  Java Applet  BEANS: Introduction to java Beans and Swings  Servlets
  • 3. Event Handling: Different Mechanism  An event can be defined as changing the state of an object or behavior by performing actions. Actions can be a button click, cursor movement, keypress through keyboard or page scrolling, etc.  The java.awt.event package can be used to provide various event classes.  Classification of Events  Foreground Events  Background Events
  • 4. Cont…..  1. Foreground Events  Foreground events are the events that require user interaction to generate, i.e., foreground events are generated due to interaction by the user on components in Graphic User Interface (GUI). Interactions are nothing but clicking on a button, scrolling the scroll bar, cursor moments, etc.  2. Background Events  Events that don’t require interactions of users to generate are known as background events. Examples of these events are operating system failures/interrupts, operation completion, etc.
  • 5. Event Handling  It is a mechanism to control the events and to decide what should happen after an event occur. To handle the events, Java follows the Delegation Event model.  Delegation Event model  It has Sources and Listeners.  Source: Events are generated from the source. There are various sources like buttons, checkboxes, list, menu-item, choice, scrollbar, text components, windows, etc., to generate events.  Listeners: Listeners are used for handling the events generated from the source. Each of these listeners represents interfaces that are responsible for handling events.  To perform Event Handling, we need to register the source with the listener.
  • 6. The delegation Event Model  The Delegation Event model is defined to handle events in GUI programming languages. The GUI stands for Graphical User Interface, where a user graphically/visually interacts with the system.  The GUI programming is inherently event-driven; whenever a user initiates an activity such as a mouse activity, clicks, scrolling, etc., each is known as an event that is mapped to a code to respond to functionality to the user. This is known as event handling.  In this section, we will discuss event processing and how to implement the delegation event model in Java. We will also discuss the different components of an Event Model.
  • 7. Event Classes  Changing the state of an object is known as an event. For example, click on button, dragging mouse etc.  The java.awt.event package provides many event classes and Listener interfaces for event handling.
  • 8. Java Event classes and Listener interfaces
  • 9. Adapter and Inner Classes  Java adapter classes provide the default implementation of listener interfaces.  If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces.  So it saves code.
  • 10. Pros of using Adapter classes:  It assists the unrelated classes to work combinedly.  It provides ways to use classes in different ways.  It increases the transparency of classes.  It provides a way to include related patterns in the class.  It provides a pluggable kit for developing an application.  It increases the reusability of the class.
  • 11. java.awt.event Adapter classes  The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages.
  • 12. Inner Classes  In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.  To access the inner class, create an object of the outer class, and then create an object of the inner class:  class OuterClass {  int x = 10;  class InnerClass {  int y = 5;  }  }
  • 13. Cont…..  public class Main {  public static void main(String[] args) {  OuterClass myOuter = new OuterClass();  OuterClass.InnerClass myInner = myOuter.new InnerClass();  System.out.println(myInner.y + myOuter.x);  }  }
  • 14. Working with windows  What is Windows Programming  Although the answer to this question always seems improper, Windows programming is the type of programming that commonly relates to performing programming on the Windows OS platform.  Thus a Windows program is that program that gets executed on Windows.  Windows Programming using Java relates to the Java programming on Graphical User Interface and provides a user-friendly environment for the user.  A user finds to work with Java more friendly and in a great way.  In simple words using and executing Java programs on Windows OS defines Windows programming.
  • 15. Windows Programming with Java  Windows Programming with Java is the utilization of the CPU, its registers, and other hardware and software devices to use and execute the task.  In Java, it has libraries that support the Windows OS.  Java Windows Programming Technique
  • 16. Java APIs  Currently, there exist the following three sets of Java APIs for performing graphics programming:  1) Abstract Windowing Toolkit (AWT): The concept of such API came into existence in JDK 1.0, in which several AWT components become obsolete as well as should be replaced by newer Swing components.  2) Swing API: A more comprehensive set of graphics libraries that enhances the AWT. This API was introduced as part of Java Foundation Classes (JFC) after the release of JDK 1.1. After the introduction of JDK 1.1, Swing API came as a part of JFC (Java Foundation Classes). It consists of Java2D, Accessibility, Swings, Internationalization, and Pluggable Look-and-Feel Support APIs. From JDK 1.2, JFC has been integrated into core Java.  3) Tool/IDE for executing Java code: Need to have Notepad++, Eclipse, IntelliJ, or Netbeans IDEs that allow a user to write and execute programs.
  • 17. Graphics and text  Graphics is an abstract class provided by Java AWT which is used to draw or paint on the components.  It consists of various fields which hold information like components to be painted, font, color, XOR mode, etc., and methods that allow drawing various shapes on the GUI components.  Graphics is an abstract class and thus cannot be initialized directly.  Objects of its child classes can be obtained in the following two ways.
  • 18. Cont….  1. Inside paint() or update() method  It is passed as an argument to paint and update methods and therefore can be accessed inside these methods. paint() and update() methods are present in the Component class and thus can be overridden for the component to be painted.  void paint(Graphics g)  void update(Graphics g)
  • 19. Cont….  import java.awt.*;  import java.awt.event.WindowAdapter;  import java.awt.event.WindowEvent;   public class MyFrame extends Frame {  public MyFrame()  {  setVisible(true);  setSize(300, 200); 
  • 20. Cont…..  addWindowListener(new WindowAdapter() {  @Override  public void windowClosing(WindowEvent e)  {  System.exit(0);  }  });
  • 21. Cont….  }  public void paint(Graphics g)  {  g.drawRect(100, 100, 100, 50);  }   public static void main(String[] args)  {  new MyFrame();  }  }
  • 22. Cont….  2. getGraphics() method  This method is present in the Component class and thus can be called on any Component in order to get the Graphics object for the component.  import java.awt.*;  import java.awt.event.WindowAdapter;  import java.awt.event.WindowEvent;   public class MyFrame extends Frame {  public MyFrame()  {  setVisible(true);
  • 23. Cont….  setSize(300, 200);  addWindowListener(new WindowAdapter() {  @Override  public void windowClosing(WindowEvent e)  {  System.exit(0);  }  });  }
  • 24. Cont….  public void paint(Graphics g)  {  System.out.println("painting...");  }   public static void main(String[] args)  {  MyFrame f = new MyFrame();  Graphics g = f.getGraphics();  System.out.println("drawing...");  g.drawRect(100, 100, 10, 10);  System.out.println("drawn...");  }  }
  • 25. Using AWT controls  The AWT supports the following types of  controls:  • Labels  • Push buttons  • Check boxes  • Choice lists  • Lists  • Scroll bars  • Text Editing  These controls are subclasses of Component.
  • 26. Layout managers and menus  The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers. There are the following classes that represent the layout managers:  java.awt.BorderLayout  java.awt.FlowLayout  java.awt.GridLayout  java.awt.CardLayout
  • 27. Cont…..  java.awt.GridBagLayout  javax.swing.BoxLayout  javax.swing.GroupLayout  javax.swing.ScrollPaneLayout  javax.swing.SpringLayout etc.
  • 28. menus  The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass.  The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.  import java.awt.*;  class MenuExample  {  MenuExample(){  Frame f= new Frame("Menu and MenuItem Example");  MenuBar mb=new MenuBar();  Menu menu=new Menu("Menu");  Menu submenu=new Menu("Sub Menu");  MenuItem i1=new MenuItem("Item 1");
  • 29. Cont….  MenuItem i2=new MenuItem("Item 2");  MenuItem i3=new MenuItem("Item 3");  MenuItem i4=new MenuItem("Item 4");  MenuItem i5=new MenuItem("Item 5");  menu.add(i1);  menu.add(i2);  menu.add(i3);  submenu.add(i4);  submenu.add(i5);  menu.add(submenu);  mb.add(menu);  f.setMenuBar(mb);
  • 30. Cont…..  f.setSize(400,400);  f.setLayout(null);  f.setVisible(true);  }  public static void main(String args[])  {  new MenuExample();  }  }
  • 31. Java Applet  An applet is a Java program that runs in a Web browser.  An applet can be a fully functional Java application because it has the entire Java API at its disposal.  An applet is a Java class that extends the java.applet.Applet class.  A main() method is not invoked on an applet, and an applet class will not define main().  Applets are designed to be embedded within an HTML page.  When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.  A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.  The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
  • 32. CONT….  Applets have strict security rules that are enforced by the Web browser. The security of an applet is often referred to as sandbox security, comparing the applet to a child playing in a sandbox with various rules that must be followed.  Other classes that the applet needs can be downloaded in a single Java Archive (JAR) file.
  • 33. Life Cycle of an Applet  Four methods in the Applet class gives you the framework on which you build any serious applet −  init − This method is iintended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.  start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.  stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
  • 34. CONT…..  destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.  paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
  • 35. A "Hello, World" Applet  import java.applet.*;  import java.awt.*;  public class HelloWorldApplet extends Applet {  public void paint (Graphics g) {  g.drawString ("Hello World", 25, 50);  }  }
  • 36. BEANS: Introduction to java Beans and Swings  A JavaBean is a Java class that should follow the following conventions:  It should have a no-arg constructor.  It should be Serializable.  It should provide methods to set and get the values of the properties, known as getter and setter methods.
  • 37. Why use JavaBean?  According to Java white paper, it is a reusable software component.  A bean encapsulates many objects into one object so that we can access this object from multiple places.  Moreover, it provides easy maintenance.
  • 38. Simple example of JavaBean class  //Employee.java   package mypack;  public class Employee implements java.io.Serializable{  private int id;  private String name;  public Employee(){}
  • 39. Cont……  public void setId(int id){this.id=id;}  public int getId(){return id;}  public void setName(String name){this.name=name;}  public String getName(){return name;}  }
  • 40. Swings  Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.  Unlike AWT, Java Swing provides platform-independent and lightweight components.  The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.  Java swing components are platform-independent.  Swing components are lightweight.  Swing supports pluggable look and feel.
  • 41. The hierarchy of java swing API is given below
  • 42. File: FirstSwingExample.java  import javax.swing.*;  public class FirstSwingExample {  public static void main(String[] args) {  JFrame f=new JFrame();//creating instance of JFrame  JButton b=new JButton("click");//creating instance of JButton  b.setBounds(130,100,100, 40);//x axis, y axis, width, height  f.add(b);//adding button in JFrame  f.setSize(400,500);//400 width and 500 height  f.setLayout(null);//using no layout managers  f.setVisible(true);//making the frame visible  }  }
  • 43. Servlets  Servlets are the Java programs that run on the Java-enabled web server or application server.  They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver.  Properties of Servlets are as follows:  Servlets work on the server-side.  Servlets are capable of handling complex requests obtained from the webserver.
  • 44. Servlet Architecture is can be depicted from the image itself as provided below as follows:
  • 45. Execution of Servlets basically involves six basic steps:  The clients send the request to the webserver.  The web server receives the request.  The web server passes the request to the corresponding servlet.  The servlet processes the request and generates the response in the form of output.  The servlet sends the response back to the webserver.  The web server sends the response back to the client and the client browser displays it on the screen.