The document provides an overview of key topics in Java including event handling, the delegation event model, event classes, listener interfaces, adapter and inner classes, working with windows, graphics and text, AWT controls, layout managers, menus, Java applets, beans, and servlets. It discusses event types, how events are handled in Java using the delegation model with sources and listeners, common event classes and interfaces, and how to draw graphics and text. It also covers using various AWT components, different layout managers, creating menus, and basics of applets, beans, and servlets.
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.
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.
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);
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);
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
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");
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.
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.
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.