SlideShare a Scribd company logo
1 of 14
Agenda
๏‚— Event Handling
๏‚— Layouts
Event Handling
๏‚— What is an Event?
๏‚— Change in the state of an object is known as event i.e. event describes the change in state of
source.
๏‚— Events are generated as result of user interaction with the graphical user interface components.
๏‚— For example, clicking on a button, moving the mouse, entering a character through
keyboard,selecting an item from list, scrolling the page are the activities that causes an event to
happen
๏‚— Types of Event
๏‚— Foreground Events
๏‚— Those events which require the direct interaction of user.
๏‚— They are generated as consequences of a person interacting with the graphical components in Graphical User
Interface.
๏‚— For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item
from list, scrolling the page etc.
๏‚— Background Events
๏‚— Those events that require the interaction of end user are known as background events.
๏‚— Operating system interrupts, hardware or software failure, timer expires, an operation completion are the
example of background events.
Event Handling
๏‚— What is Event Handling?
๏‚— Event Handling is the mechanism that controls the event and decides what should happen if an event occurs.
๏‚— This mechanism have the code which is known as event handler that is executed when an event occurs.
๏‚— Java Uses the Delegation Event Model to handle the events.
๏‚— This model defines the standard mechanism to generate and handle the events.Let's have a brief introduction to this
model.
๏‚— The Delegation Event Model has the following key participants namely:
๏‚— Source - The source is an object on which event occurs. Source is responsible for providing information of the occurred
event to it's handler. Java provide as with classes for source object.
๏‚— Listener - It is also known as event handler.Listener is responsible for generating response to an event. From java
implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is
received , the listener process the event an then returns.
๏‚— The benefit of this approach is that the user interface logic is completely separated from the logic that generates the
event.
๏‚— The user interface element is able to delegate the processing of an event to the separate piece of code.
๏‚— In this model ,Listener needs to be registered with the source object so that the listener can receive the event
notification.
๏‚— This is an efficient way of handling the event because the event notifications are sent only to those listener that want to
receive them.
Event Handling
๏‚— Steps involved in event handling
๏‚— The User clicks the button and the event is generated.
๏‚— Now the object of concerned event class is created automatically and information about the source and the
event get populated with in same object.
๏‚— Event object is forwarded to the method of registered listener class.
๏‚— the method is now get executed and returns.
๏‚— Points to remember about listener
๏‚— In order to design a listener class we have to develop some listener interfaces.These Listener interfaces
forecast some public abstract callback methods which must be implemented by the listener class.
๏‚— If you do not implement the any if the predefined interfaces then your class can not act as a listener class for
a source object.
๏‚— Callback Methods
๏‚— These are the methods that are provided by API provider and are defined by the application programmer
and invoked by the application developer. Here the callback methods represents an event method. In
response to an event java jre will fire callback method. All such callback methods are provided in listener
interfaces.
๏‚— If a component wants some listener will listen to it's events the the source must register itself to the listener.
Event Handling
๏‚— Steps involved in event handling
๏‚— The User clicks the button and the event is generated.
๏‚— Now the object of concerned event class is created automatically and information about the source and the
event get populated with in same object.
๏‚— Event object is forwarded to the method of registered listener class.
๏‚— the method is now get executed and returns.
๏‚— Points to remember about listener
๏‚— In order to design a listener class we have to develop some listener interfaces.These Listener interfaces
forecast some public abstract callback methods which must be implemented by the listener class.
๏‚— If you do not implement the any if the predefined interfaces then your class can not act as a listener class for
a source object.
๏‚— Callback Methods
๏‚— These are the methods that are provided by API provider and are defined by the application programmer
and invoked by the application developer. Here the callback methods represents an event method. In
response to an event java jre will fire callback method. All such callback methods are provided in listener
interfaces.
๏‚— If a component wants some listener will listen to it's events the the source must register itself to the listener.
Event Handling โ€“AWT Example
import java.awt.*;
import java.awt.event.*;
public class AwtControlDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public AwtControlDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtControlDemo awtControlDemo = new AwtControlDemo();
awtControlDemo.showEventDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel = new Panel();
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
Button okButton = new Button("OK");
Button submitButton = new Button("Submit");
Button cancelButton = new Button("Cancel");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "OK" )) {
statusLabel.setText("Ok Button clicked.");
}
else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button clicked.");
}
else {
statusLabel.setText("Cancel Button clicked.");
}
}
}
Event Handling โ€“SWT Example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingControlDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingControlDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingControlDemo swingControlDemo = new
SwingControlDemo();
swingControlDemo.showEventDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
JButton okButton = new JButton("OK");
JButton submitButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "OK" )) {
statusLabel.setText("Ok Button clicked.");
} else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button clicked.");
} else {
statusLabel.setText("Cancel Button clicked.");
}
}
}
}
Event Handling
๏‚— The Event classes represent the event. Java provides us various Event classes but we
will discuss those which are more frequently used.
๏‚— AWT & Swing Events.
๏‚— AWTEvent
๏‚— ActionEvent
๏‚— InputEvent
๏‚— KeyEvent
๏‚— MouseEvent
๏‚— TextEvent
๏‚— WindowEvent
๏‚— AdjustmentEvent
๏‚— ComponentEvent
๏‚— ContainerEvent
๏‚— MouseMotionEvent
๏‚— PaintEvent
Event Handling
๏‚— The Event listener represent the interfaces responsible to handle events.
๏‚— Java provides us various Event listener classes but we will discuss those which are more frequently used.
๏‚— Every method of an event listener method has a single argument as an object which is subclass of EventObject class.
๏‚— For example, mouse event listener methods will accept instance of MouseEvent, where MouseEvent derives from
EventObject.
๏‚— Event Listeners:
๏‚— ActionListener
๏‚— ComponentListener
๏‚— ItemListener
๏‚— KeyListener
๏‚— MouseListener
๏‚— TextListener
๏‚— WindowListener
๏‚— AdjustmentListener
๏‚— ContainerListener
๏‚— MouseMotionListener
๏‚— FocusListener
Layouts
๏‚— Layout means the arrangement of components within the container.
๏‚— In other way we can say that placing the components at a particular position within the container.
๏‚— The task of layouting the controls is done automatically by the Layout Manager.
๏‚— The layout manager automatically positions all the components within the container.
๏‚— If we do not use layout manager then also the components are positioned by the default layout manager.
๏‚— It is possible to layout the controls by hand but it becomes very difficult because of the following two reasons.
๏‚— It is very tedious to handle a large number of controls within the container.
๏‚— Oftenly the width and height information of a component is not given when we need to arrange them.
๏‚— Java provide us with various layout manager to position the controls.
๏‚— The properties like size,shape and arrangement varies from one layout manager to other layout manager.
๏‚— When the size of the applet or the application window changes the size, shape and arrangement of the components also changes in
response i.e. the layout managers adapt to the dimensions of appletviewer or the application window.
Layouts
๏‚— Layout Manager Classes
๏‚— BorderLayout - The borderlayout arranges the components to fit in the five regions:
east, west, north, south and center.
๏‚— CardLayout - The CardLayout object treats each component in the container as a
card. Only one card is visible at a time.
๏‚— FlowLayout - The FlowLayout is the default layout.It layouts the components in a
directional flow.
๏‚— GridLayout - The GridLayout manages the components in form of a rectangular
grid.
๏‚— GridBagLayout - This is the most flexible layout manager class.The object of
GridBagLayout aligns the component vertically,horizontally or along their baseline
without requiring the components of same size.
Layouts โ€“ AWT Example
import java.awt.*;
import java.awt.event.*;
public class AwtLayoutDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
private Label msglabel;
public AwtLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo();
awtLayoutDemo.showBorderLayoutDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
msglabel = new Label();
msglabel.setAlignment(Label.CENTER);
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showBorderLayoutDemo(){
headerLabel.setText("Layout in action: BorderLayout");
Panel panel = new Panel();
panel.setBackground(Color.darkGray);
panel.setSize(300,300);
BorderLayout layout = new BorderLayout();
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout);
panel.add(new Button("Center"),BorderLayout.CENTER);
panel.add(new Button("Line Start"),BorderLayout.LINE_START);
panel.add(new Button("Line End"),BorderLayout.LINE_END);
panel.add(new Button("East"),BorderLayout.EAST);
panel.add(new Button("West"),BorderLayout.WEST);
panel.add(new Button("North"),BorderLayout.NORTH);
panel.add(new Button("South"),BorderLayout.SOUTH);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
Layouts โ€“ Swing Example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingLayoutDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingLayoutDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();
swingLayoutDemo.showFlowLayoutDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showFlowLayoutDemo(){
headerLabel.setText("Layout in action: FlowLayout");
JPanel panel = new JPanel();
panel.setBackground(Color.darkGray);
panel.setSize(200,200);
FlowLayout layout = new FlowLayout();
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout);
panel.add(new JButton("OK"));
panel.add(new JButton("Cancel"));
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}

More Related Content

Similar to ITE 1122_ Event Handling.pptx

java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
ย 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
ย 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)RAJITHARAMACHANDRAN1
ย 
What is Event
What is EventWhat is Event
What is EventAsmita Prasad
ย 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
ย 
PATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelPATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelMichael Heron
ย 
Swiss army knife Spring
Swiss army knife SpringSwiss army knife Spring
Swiss army knife SpringMario Fusco
ย 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
ย 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
ย 
Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)Satish Verma
ย 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)jammiashok123
ย 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxTadeseBeyene
ย 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
ย 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
ย 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandlingArati Gadgil
ย 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programmingSynapseindiappsdevelopment
ย 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2PRN USM
ย 

Similar to ITE 1122_ Event Handling.pptx (20)

java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
ย 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
ย 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
ย 
What is Event
What is EventWhat is Event
What is Event
ย 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
ย 
PATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event ModelPATTERNS06 - The .NET Event Model
PATTERNS06 - The .NET Event Model
ย 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
ย 
Swiss army knife Spring
Swiss army knife SpringSwiss army knife Spring
Swiss army knife Spring
ย 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
ย 
Lecture 18
Lecture 18Lecture 18
Lecture 18
ย 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
ย 
Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
ย 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
ย 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
ย 
EventHandling.pptx
EventHandling.pptxEventHandling.pptx
EventHandling.pptx
ย 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
ย 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
ย 
Java eventhandling
Java eventhandlingJava eventhandling
Java eventhandling
ย 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
ย 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
ย 

Recently uploaded

Toronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfToronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfJinJiang6
ย 
Bhubaneswar๐ŸŒนKalpana Mesuem โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...
Bhubaneswar๐ŸŒนKalpana Mesuem  โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...Bhubaneswar๐ŸŒนKalpana Mesuem  โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...
Bhubaneswar๐ŸŒนKalpana Mesuem โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...Call Girls Mumbai
ย 
โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...Call Girls Mumbai
ย 
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...batoole333
ย 
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize ThemSignificant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them360factors
ย 
fundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docxfundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docxssuserf63bd7
ย 
cost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxcost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxazadalisthp2020i
ย 
falcon-invoice-discounting-unlocking-prime-investment-opportunities
falcon-invoice-discounting-unlocking-prime-investment-opportunitiesfalcon-invoice-discounting-unlocking-prime-investment-opportunities
falcon-invoice-discounting-unlocking-prime-investment-opportunitiesFalcon Invoice Discounting
ย 
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call GirlsKurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call GirlsPriya Reddy
ย 
Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...
Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...
Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...kajal
ย 
Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...
Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...
Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...priyasharma62062
ย 
Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...
Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...
Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...sanakhan51485
ย 
Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...
Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...
Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...vershagrag
ย 
Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...robinsonayot
ย 
Pension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdfPension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdfHenry Tapper
ย 
Technology industry / Finnish economic outlook
Technology industry / Finnish economic outlookTechnology industry / Finnish economic outlook
Technology industry / Finnish economic outlookTechFinland
ย 
logistics industry development power point ppt.pdf
logistics industry development power point ppt.pdflogistics industry development power point ppt.pdf
logistics industry development power point ppt.pdfSalimullah13
ย 
Vip Call Girls Rasulgada๐Ÿ˜‰ Bhubaneswar 9777949614 Housewife Call Girls Servic...
Vip Call Girls Rasulgada๐Ÿ˜‰  Bhubaneswar 9777949614 Housewife Call Girls Servic...Vip Call Girls Rasulgada๐Ÿ˜‰  Bhubaneswar 9777949614 Housewife Call Girls Servic...
Vip Call Girls Rasulgada๐Ÿ˜‰ Bhubaneswar 9777949614 Housewife Call Girls Servic...Call Girls Mumbai
ย 
Call Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budget
Call Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budgetCall Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budget
Call Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budgetSareena Khatun
ย 
โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...jabtakhaidam7
ย 

Recently uploaded (20)

Toronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfToronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdf
ย 
Bhubaneswar๐ŸŒนKalpana Mesuem โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...
Bhubaneswar๐ŸŒนKalpana Mesuem  โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...Bhubaneswar๐ŸŒนKalpana Mesuem  โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...
Bhubaneswar๐ŸŒนKalpana Mesuem โคCALL GIRLS 9777949614 ๐Ÿ’Ÿ CALL GIRLS IN bhubaneswa...
ย 
โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
โœ‚๏ธ ๐Ÿ‘… Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
ย 
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
ย 
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize ThemSignificant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them
ย 
fundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docxfundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docx
ย 
cost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxcost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptx
ย 
falcon-invoice-discounting-unlocking-prime-investment-opportunities
falcon-invoice-discounting-unlocking-prime-investment-opportunitiesfalcon-invoice-discounting-unlocking-prime-investment-opportunities
falcon-invoice-discounting-unlocking-prime-investment-opportunities
ย 
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call GirlsKurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
ย 
Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...
Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...
Call Girls in Benson Town / 8250092165 Genuine Call girls with real Photos an...
ย 
Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...
Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...
Female Russian Escorts Mumbai Call Girls-((ANdheri))9833754194-Jogeshawri Fre...
ย 
Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...
Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...
Escorts Indore Call Girls-9155612368-Vijay Nagar Decent Fantastic Call Girls ...
ย 
Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...
Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...
Premium Call Girls Bangalore Call Girls Service Just Call ๐Ÿ‘๐Ÿ‘„6378878445 ๐Ÿ‘๐Ÿ‘„ Top...
ย 
Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...Test bank for advanced assessment interpreting findings and formulating diffe...
Test bank for advanced assessment interpreting findings and formulating diffe...
ย 
Pension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdfPension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdf
ย 
Technology industry / Finnish economic outlook
Technology industry / Finnish economic outlookTechnology industry / Finnish economic outlook
Technology industry / Finnish economic outlook
ย 
logistics industry development power point ppt.pdf
logistics industry development power point ppt.pdflogistics industry development power point ppt.pdf
logistics industry development power point ppt.pdf
ย 
Vip Call Girls Rasulgada๐Ÿ˜‰ Bhubaneswar 9777949614 Housewife Call Girls Servic...
Vip Call Girls Rasulgada๐Ÿ˜‰  Bhubaneswar 9777949614 Housewife Call Girls Servic...Vip Call Girls Rasulgada๐Ÿ˜‰  Bhubaneswar 9777949614 Housewife Call Girls Servic...
Vip Call Girls Rasulgada๐Ÿ˜‰ Bhubaneswar 9777949614 Housewife Call Girls Servic...
ย 
Call Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budget
Call Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budgetCall Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budget
Call Girls Howrah ( 8250092165 ) Cheap rates call girls | Get low budget
ย 
โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
โœ‚๏ธ ๐Ÿ‘… Independent Lucknow Escorts U.P Call Girls With Room Lucknow Call Girls ...
ย 

ITE 1122_ Event Handling.pptx

  • 1.
  • 3. Event Handling ๏‚— What is an Event? ๏‚— Change in the state of an object is known as event i.e. event describes the change in state of source. ๏‚— Events are generated as result of user interaction with the graphical user interface components. ๏‚— For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page are the activities that causes an event to happen ๏‚— Types of Event ๏‚— Foreground Events ๏‚— Those events which require the direct interaction of user. ๏‚— They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. ๏‚— For example, clicking on a button, moving the mouse, entering a character through keyboard,selecting an item from list, scrolling the page etc. ๏‚— Background Events ๏‚— Those events that require the interaction of end user are known as background events. ๏‚— Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.
  • 4. Event Handling ๏‚— What is Event Handling? ๏‚— Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. ๏‚— This mechanism have the code which is known as event handler that is executed when an event occurs. ๏‚— Java Uses the Delegation Event Model to handle the events. ๏‚— This model defines the standard mechanism to generate and handle the events.Let's have a brief introduction to this model. ๏‚— The Delegation Event Model has the following key participants namely: ๏‚— Source - The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's handler. Java provide as with classes for source object. ๏‚— Listener - It is also known as event handler.Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received , the listener process the event an then returns. ๏‚— The benefit of this approach is that the user interface logic is completely separated from the logic that generates the event. ๏‚— The user interface element is able to delegate the processing of an event to the separate piece of code. ๏‚— In this model ,Listener needs to be registered with the source object so that the listener can receive the event notification. ๏‚— This is an efficient way of handling the event because the event notifications are sent only to those listener that want to receive them.
  • 5. Event Handling ๏‚— Steps involved in event handling ๏‚— The User clicks the button and the event is generated. ๏‚— Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. ๏‚— Event object is forwarded to the method of registered listener class. ๏‚— the method is now get executed and returns. ๏‚— Points to remember about listener ๏‚— In order to design a listener class we have to develop some listener interfaces.These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class. ๏‚— If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object. ๏‚— Callback Methods ๏‚— These are the methods that are provided by API provider and are defined by the application programmer and invoked by the application developer. Here the callback methods represents an event method. In response to an event java jre will fire callback method. All such callback methods are provided in listener interfaces. ๏‚— If a component wants some listener will listen to it's events the the source must register itself to the listener.
  • 6. Event Handling ๏‚— Steps involved in event handling ๏‚— The User clicks the button and the event is generated. ๏‚— Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. ๏‚— Event object is forwarded to the method of registered listener class. ๏‚— the method is now get executed and returns. ๏‚— Points to remember about listener ๏‚— In order to design a listener class we have to develop some listener interfaces.These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class. ๏‚— If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object. ๏‚— Callback Methods ๏‚— These are the methods that are provided by API provider and are defined by the application programmer and invoked by the application developer. Here the callback methods represents an event method. In response to an event java jre will fire callback method. All such callback methods are provided in listener interfaces. ๏‚— If a component wants some listener will listen to it's events the the source must register itself to the listener.
  • 7. Event Handling โ€“AWT Example import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showEventDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); private void showEventDemo(){ headerLabel.setText("Control in action: Button"); Button okButton = new Button("OK"); Button submitButton = new Button("Submit"); Button cancelButton = new Button("Cancel"); okButton.setActionCommand("OK"); submitButton.setActionCommand("Submit"); cancelButton.setActionCommand("Cancel"); okButton.addActionListener(new ButtonClickListener()); submitButton.addActionListener(new ButtonClickListener()); cancelButton.addActionListener(new ButtonClickListener()); controlPanel.add(okButton); controlPanel.add(submitButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } private class ButtonClickListener implements ActionListener{ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if( command.equals( "OK" )) { statusLabel.setText("Ok Button clicked."); } else if( command.equals( "Submit" ) ) { statusLabel.setText("Submit Button clicked."); } else { statusLabel.setText("Cancel Button clicked."); } } }
  • 8. Event Handling โ€“SWT Example import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showEventDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); private void showEventDemo(){ headerLabel.setText("Control in action: Button"); JButton okButton = new JButton("OK"); JButton submitButton = new JButton("Submit"); JButton cancelButton = new JButton("Cancel"); okButton.setActionCommand("OK"); submitButton.setActionCommand("Submit"); cancelButton.setActionCommand("Cancel"); okButton.addActionListener(new ButtonClickListener()); submitButton.addActionListener(new ButtonClickListener()); cancelButton.addActionListener(new ButtonClickListener()); controlPanel.add(okButton); controlPanel.add(submitButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } private class ButtonClickListener implements ActionListener{ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if( command.equals( "OK" )) { statusLabel.setText("Ok Button clicked."); } else if( command.equals( "Submit" ) ) { statusLabel.setText("Submit Button clicked."); } else { statusLabel.setText("Cancel Button clicked."); } } } }
  • 9. Event Handling ๏‚— The Event classes represent the event. Java provides us various Event classes but we will discuss those which are more frequently used. ๏‚— AWT & Swing Events. ๏‚— AWTEvent ๏‚— ActionEvent ๏‚— InputEvent ๏‚— KeyEvent ๏‚— MouseEvent ๏‚— TextEvent ๏‚— WindowEvent ๏‚— AdjustmentEvent ๏‚— ComponentEvent ๏‚— ContainerEvent ๏‚— MouseMotionEvent ๏‚— PaintEvent
  • 10. Event Handling ๏‚— The Event listener represent the interfaces responsible to handle events. ๏‚— Java provides us various Event listener classes but we will discuss those which are more frequently used. ๏‚— Every method of an event listener method has a single argument as an object which is subclass of EventObject class. ๏‚— For example, mouse event listener methods will accept instance of MouseEvent, where MouseEvent derives from EventObject. ๏‚— Event Listeners: ๏‚— ActionListener ๏‚— ComponentListener ๏‚— ItemListener ๏‚— KeyListener ๏‚— MouseListener ๏‚— TextListener ๏‚— WindowListener ๏‚— AdjustmentListener ๏‚— ContainerListener ๏‚— MouseMotionListener ๏‚— FocusListener
  • 11. Layouts ๏‚— Layout means the arrangement of components within the container. ๏‚— In other way we can say that placing the components at a particular position within the container. ๏‚— The task of layouting the controls is done automatically by the Layout Manager. ๏‚— The layout manager automatically positions all the components within the container. ๏‚— If we do not use layout manager then also the components are positioned by the default layout manager. ๏‚— It is possible to layout the controls by hand but it becomes very difficult because of the following two reasons. ๏‚— It is very tedious to handle a large number of controls within the container. ๏‚— Oftenly the width and height information of a component is not given when we need to arrange them. ๏‚— Java provide us with various layout manager to position the controls. ๏‚— The properties like size,shape and arrangement varies from one layout manager to other layout manager. ๏‚— When the size of the applet or the application window changes the size, shape and arrangement of the components also changes in response i.e. the layout managers adapt to the dimensions of appletviewer or the application window.
  • 12. Layouts ๏‚— Layout Manager Classes ๏‚— BorderLayout - The borderlayout arranges the components to fit in the five regions: east, west, north, south and center. ๏‚— CardLayout - The CardLayout object treats each component in the container as a card. Only one card is visible at a time. ๏‚— FlowLayout - The FlowLayout is the default layout.It layouts the components in a directional flow. ๏‚— GridLayout - The GridLayout manages the components in form of a rectangular grid. ๏‚— GridBagLayout - This is the most flexible layout manager class.The object of GridBagLayout aligns the component vertically,horizontally or along their baseline without requiring the components of same size.
  • 13. Layouts โ€“ AWT Example import java.awt.*; import java.awt.event.*; public class AwtLayoutDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; private Label msglabel; public AwtLayoutDemo(){ prepareGUI(); } public static void main(String[] args){ AwtLayoutDemo awtLayoutDemo = new AwtLayoutDemo(); awtLayoutDemo.showBorderLayoutDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); msglabel = new Label(); msglabel.setAlignment(Label.CENTER); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showBorderLayoutDemo(){ headerLabel.setText("Layout in action: BorderLayout"); Panel panel = new Panel(); panel.setBackground(Color.darkGray); panel.setSize(300,300); BorderLayout layout = new BorderLayout(); layout.setHgap(10); layout.setVgap(10); panel.setLayout(layout); panel.add(new Button("Center"),BorderLayout.CENTER); panel.add(new Button("Line Start"),BorderLayout.LINE_START); panel.add(new Button("Line End"),BorderLayout.LINE_END); panel.add(new Button("East"),BorderLayout.EAST); panel.add(new Button("West"),BorderLayout.WEST); panel.add(new Button("North"),BorderLayout.NORTH); panel.add(new Button("South"),BorderLayout.SOUTH); controlPanel.add(panel); mainFrame.setVisible(true); } }
  • 14. Layouts โ€“ Swing Example import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingLayoutDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; private JLabel msglabel; public SwingLayoutDemo(){ prepareGUI(); } public static void main(String[] args){ SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo(); swingLayoutDemo.showFlowLayoutDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showFlowLayoutDemo(){ headerLabel.setText("Layout in action: FlowLayout"); JPanel panel = new JPanel(); panel.setBackground(Color.darkGray); panel.setSize(200,200); FlowLayout layout = new FlowLayout(); layout.setHgap(10); layout.setVgap(10); panel.setLayout(layout); panel.add(new JButton("OK")); panel.add(new JButton("Cancel")); controlPanel.add(panel); mainFrame.setVisible(true); } }