SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
» GUI
˃ Events
˃ Event Handling

https://www.facebook.com/Oxus20

oxus20@gmail.com

JAVA
GUI
PART III
Milad Kawesh
Agenda
» Events
» Java Delegation Model
» Event Handling
» Practical Examples
2

https://www.facebook.com/Oxus20
Windows based Java Programs
» Console-Based Programming
˃ Every thing is predetermined
˃ The program code determines the sequence of events

» Window-Based Programming
˃ The operation is driven by what you do with the GUI
˃ Selecting menu items, buttons, or keyboard causes particular
actions within a program
˃ The specific program that is executed next is not known
https://www.facebook.com/Oxus20

3
Event driven Programming
» The signals that a program receives from the OS as a
result of your actions are called events
» A window based program is called event driven
program
» Unlike the old rigid old sequential programs,
» it puts user in charge, user control the sequence of
program
» Application waits for the user action
» This approach is called event driven programming
4

https://www.facebook.com/Oxus20
The Event Handling Process
» Suppose a user clicks a button in the GUI
˃ Button is the source of the event

» When a button is clicked, it will create a new
object that have information about event and
its source (in this case ActionListener)
» This object is passed to a method that handles
the event in its listener
» A listener is called Target of an event
5

https://www.facebook.com/Oxus20
Delegation Event Model
» The way in which events are handled in Java, using listener
objects, is called delegation event model
» We can make objects of any class listener objects by making
the class implement a listener interface
» In case of the button, the ActionListener interface needs to

be implemented to receive events from button
» actionPerformed(ActionEvent e) is called when the event
occurs and the event object is passed as an argument
https://www.facebook.com/Oxus20

6
Java Events
» Events are objects
˃ Objects that represent user initiated actions
˃ Examples:
+ button clicked -> ActionEvent
+ mouse dragged -> MouseEvent
+ Enter key pressed -> KeyEvent
˃ EventObject; root event class for all event objects
˃ AWTEvent; root event class for all AWT events
˃ Package java.awt.event
+ Provides interfaces and classes for dealing with different
types of events fired by AWT components.
7

https://www.facebook.com/Oxus20
8

https://www.facebook.com/Oxus20
Event Handling
» Programmer choice to decide how to handle the
generated event
˃ Ignore the Event
˃ Have the Event handled by the component where the
event was generated (Self Contained Event handling)
˃ Delegate event handling to some other object called
Listeners (Event Delegation)
9

https://www.facebook.com/Oxus20
Event Delegation
» Some time component on which event was
generated is not best suited to handle its own
event
» The process of assigning an object to handle a

component’s events is called delegation.
» The event handling objects are called Listeners
10

https://www.facebook.com/Oxus20
Key Methods of Event
» Object getSource()
In ObjectEvent, return the component in
which event took place.
» int getID()

In AwtEvent, return int that describes the
nature of the event e.g. on MouseEvent it will
give MOUSE_PRESSED, MOUSE_DRAGGED.
11

https://www.facebook.com/Oxus20
Event Listeners
» Interfaces to support dispatching of events
» Each Event class has a corresponding Listener interface
» Multiple listeners for the same event type
» Each interface will have one or more method

corresponding to types of events
» ActionEvent -> ActionListener
» MouseEvent -> MouseListener and MouseMotionListener
https://www.facebook.com/Oxus20

12
Registering Listeners
» Listeners register themselves with component
˃ public void addXXXListener(XXXListener)
˃ addActionListener, addItemListener, etc.

» Multiple listeners can be registered for the same

event on a component
˃ One event can trigger numerous responses
˃ Events are broadcast to all listeners
13

https://www.facebook.com/Oxus20
Wiring a Listener
» Define a class to implement the Listener Interface
Public class Applet extends Applet implements ActionListener
public class MyClass implements ActionListener {

» Add the implementation of the Interface
…
public void actionPerformed(ActionEvent e) {
// here’s where I do stuff when the action happens
…

» Add class as a Listener to Component
…
Button ok = new Button(“OK”)
ok.addActionListener(this);
14

https://www.facebook.com/Oxus20
ActionListener Example
import
import
import
import
import

javax.swing.*;
java.awt.FlowLayout;
java.awt.Dimension;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;

public class ActionListenerTest extends JFrame implements
ActionListener {

JTextArea topTextArea;
JTextArea bottomTextArea;
JButton button1, button2;
final static String newline = "n";
15

https://www.facebook.com/Oxus20
ActionListener Example (cont.)
public ActionListenerTest() {
setLayout(new FlowLayout());
topTextArea = new JTextArea();
topTextArea.setEditable(false);
JScrollPane topScrollPane = new JScrollPane(topTextArea);
topScrollPane.setPreferredSize(new Dimension(200, 75));
add(topScrollPane);
bottomTextArea = new JTextArea();
bottomTextArea.setEditable(false);
JScrollPane bottomScrollPane = new JScrollPane(bottomTextArea);

bottomScrollPane.setPreferredSize(new Dimension(200, 75));
16

https://www.facebook.com/Oxus20
ActionListener Example (cont.)
add(bottomScrollPane);
button1 = new JButton("top Text area");
add(button1);
button2 = new JButton("down text area");
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
setSize(300, 222);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
17

https://www.facebook.com/Oxus20
ActionListener Example(cont.)
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
topTextArea.append(e.getActionCommand() + newline);
}
if (e.getSource() == button2) {
bottomTextArea.append(e.getActionCommand() + newline);
}
}
public static void main(String[] args) {
new ActionListenerTest();
}
}
18

https://www.facebook.com/Oxus20
OUTPUT

19

https://www.facebook.com/Oxus20
Lets practice

20

https://www.facebook.com/Oxus20
END

21

https://www.facebook.com/Oxus20

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

GUI (graphical user interface)
GUI (graphical user interface)GUI (graphical user interface)
GUI (graphical user interface)
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Event handling63
Event handling63Event handling63
Event handling63
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
28 awt
28 awt28 awt
28 awt
 
Event handling in Java(part 2)
Event handling in Java(part 2)Event handling in Java(part 2)
Event handling in Java(part 2)
 
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
 
Complete java swing
Complete java swingComplete java swing
Complete java swing
 
tL19 awt
tL19 awttL19 awt
tL19 awt
 
09events
09events09events
09events
 
25 awt
25 awt25 awt
25 awt
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 
What is Event
What is EventWhat is Event
What is Event
 
Swings in java
Swings in javaSwings in java
Swings in java
 
Java swing
Java swingJava swing
Java swing
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 

Andere mochten auch

JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART IOXUS 20
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART IIOXUS 20
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Javayht4ever
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Javababak danyal
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A ReviewFernando Torres
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1PRN USM
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)Bilal Amjad
 
Threads and concurrency in Java 1.5
Threads and concurrency in Java 1.5Threads and concurrency in Java 1.5
Threads and concurrency in Java 1.5Peter Antman
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaRuben Inoto Soto
 
Utilisation de ZK avec Java - Retour d’expérience
Utilisation de ZK avec Java - Retour d’expérienceUtilisation de ZK avec Java - Retour d’expérience
Utilisation de ZK avec Java - Retour d’expériencelouschwartz
 
Introduction of Android Camera1
Introduction of Android Camera1Introduction of Android Camera1
Introduction of Android Camera1Booch Lin
 
Oracle ADF : Vue d'ensemble
Oracle ADF : Vue d'ensembleOracle ADF : Vue d'ensemble
Oracle ADF : Vue d'ensembleANASYS
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event HandlingJava Programming
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transferAnkit Desai
 
java swing programming
java swing programming java swing programming
java swing programming Ankit Desai
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesOXUS 20
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationOXUS 20
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaOXUS 20
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement OXUS 20
 

Andere mochten auch (20)

JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART II
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Swing and Graphical User Interface in Java
Swing and Graphical User Interface in JavaSwing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) -  A ReviewGUI Programming in JAVA (Using Netbeans) -  A Review
GUI Programming in JAVA (Using Netbeans) - A Review
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 
Threads and concurrency in Java 1.5
Threads and concurrency in Java 1.5Threads and concurrency in Java 1.5
Threads and concurrency in Java 1.5
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Utilisation de ZK avec Java - Retour d’expérience
Utilisation de ZK avec Java - Retour d’expérienceUtilisation de ZK avec Java - Retour d’expérience
Utilisation de ZK avec Java - Retour d’expérience
 
Introduction of Android Camera1
Introduction of Android Camera1Introduction of Android Camera1
Introduction of Android Camera1
 
Oracle ADF : Vue d'ensemble
Oracle ADF : Vue d'ensembleOracle ADF : Vue d'ensemble
Oracle ADF : Vue d'ensemble
 
Java swings
Java swingsJava swings
Java swings
 
Java programming-Event Handling
Java programming-Event HandlingJava programming-Event Handling
Java programming-Event Handling
 
java drag and drop and data transfer
java drag and drop and data transferjava drag and drop and data transfer
java drag and drop and data transfer
 
java swing programming
java swing programming java swing programming
java swing programming
 
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton ClassesJava Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
Java Virtual Keyboard Using Robot, Toolkit and JToggleButton Classes
 
PHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail ExplanationPHP Basic and Fundamental Questions and Answers with Detail Explanation
PHP Basic and Fundamental Questions and Answers with Detail Explanation
 
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using JavaFal-e-Hafez (Omens of Hafez) Cards in Persian using Java
Fal-e-Hafez (Omens of Hafez) Cards in Persian using Java
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
 

Ähnlich wie JAVA GUI PART III

Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
File Handling
File HandlingFile Handling
File HandlingSohanur63
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application DevelopmentMuhammad Sajid
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events WebStackAcademy
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024kashyapneha2809
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024nehakumari0xf
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
Discuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docxDiscuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docxwviola
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9DanWooster1
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intentPERKYTORIALS
 
Event handling
Event handlingEvent handling
Event handlingswapnac12
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Androidma-polimi
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Androidgreenrobot
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxudithaisur
 

Ähnlich wie JAVA GUI PART III (20)

Java gui event
Java gui eventJava gui event
Java gui event
 
Javascript Browser Events.pdf
Javascript Browser Events.pdfJavascript Browser Events.pdf
Javascript Browser Events.pdf
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
File Handling
File HandlingFile Handling
File Handling
 
Mobile Application Development
Mobile Application DevelopmentMobile Application Development
Mobile Application Development
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
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
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Discuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docxDiscuss how each of the following Java components will assist in creat.docx
Discuss how each of the following Java components will assist in creat.docx
 
Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9Upstate CSCI 450 WebDev Chapter 9
Upstate CSCI 450 WebDev Chapter 9
 
B2. activity and intent
B2. activity and intentB2. activity and intent
B2. activity and intent
 
Event handling
Event handlingEvent handling
Event handling
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
EventBus for Android
EventBus for AndroidEventBus for Android
EventBus for Android
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 

Mehr von OXUS 20

Java Arrays
Java ArraysJava Arrays
Java ArraysOXUS 20
 
Java Methods
Java MethodsJava Methods
Java MethodsOXUS 20
 
Structure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryStructure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryOXUS 20
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and GraphicsOXUS 20
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersOXUS 20
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsOXUS 20
 
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepOXUS 20
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesOXUS 20
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesOXUS 20
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART IIOXUS 20
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART IOXUS 20
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number TutorialOXUS 20
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIOXUS 20
 
Object Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesObject Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesOXUS 20
 
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticObject Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticOXUS 20
 

Mehr von OXUS 20 (15)

Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Structure programming – Java Programming – Theory
Structure programming – Java Programming – TheoryStructure programming – Java Programming – Theory
Structure programming – Java Programming – Theory
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Fundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and AnswersFundamentals of Database Systems Questions and Answers
Fundamentals of Database Systems Questions and Answers
 
Everything about Database JOINS and Relationships
Everything about Database JOINS and RelationshipsEverything about Database JOINS and Relationships
Everything about Database JOINS and Relationships
 
Create Splash Screen with Java Step by Step
Create Splash Screen with Java Step by StepCreate Splash Screen with Java Step by Step
Create Splash Screen with Java Step by Step
 
Web Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and TechnologiesWeb Design and Development Life Cycle and Technologies
Web Design and Development Life Cycle and Technologies
 
Java Unicode with Cool GUI Examples
Java Unicode with Cool GUI ExamplesJava Unicode with Cool GUI Examples
Java Unicode with Cool GUI Examples
 
Java Regular Expression PART II
Java Regular Expression PART IIJava Regular Expression PART II
Java Regular Expression PART II
 
Java Regular Expression PART I
Java Regular Expression PART IJava Regular Expression PART I
Java Regular Expression PART I
 
Java Guessing Game Number Tutorial
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
 
JAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART IIIJAVA Programming Questions and Answers PART III
JAVA Programming Questions and Answers PART III
 
Object Oriented Programming with Real World Examples
Object Oriented Programming with Real World ExamplesObject Oriented Programming with Real World Examples
Object Oriented Programming with Real World Examples
 
Object Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non StaticObject Oriented Concept Static vs. Non Static
Object Oriented Concept Static vs. Non Static
 

Kürzlich hochgeladen

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Kürzlich hochgeladen (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

JAVA GUI PART III

  • 1. » GUI ˃ Events ˃ Event Handling https://www.facebook.com/Oxus20 oxus20@gmail.com JAVA GUI PART III Milad Kawesh
  • 2. Agenda » Events » Java Delegation Model » Event Handling » Practical Examples 2 https://www.facebook.com/Oxus20
  • 3. Windows based Java Programs » Console-Based Programming ˃ Every thing is predetermined ˃ The program code determines the sequence of events » Window-Based Programming ˃ The operation is driven by what you do with the GUI ˃ Selecting menu items, buttons, or keyboard causes particular actions within a program ˃ The specific program that is executed next is not known https://www.facebook.com/Oxus20 3
  • 4. Event driven Programming » The signals that a program receives from the OS as a result of your actions are called events » A window based program is called event driven program » Unlike the old rigid old sequential programs, » it puts user in charge, user control the sequence of program » Application waits for the user action » This approach is called event driven programming 4 https://www.facebook.com/Oxus20
  • 5. The Event Handling Process » Suppose a user clicks a button in the GUI ˃ Button is the source of the event » When a button is clicked, it will create a new object that have information about event and its source (in this case ActionListener) » This object is passed to a method that handles the event in its listener » A listener is called Target of an event 5 https://www.facebook.com/Oxus20
  • 6. Delegation Event Model » The way in which events are handled in Java, using listener objects, is called delegation event model » We can make objects of any class listener objects by making the class implement a listener interface » In case of the button, the ActionListener interface needs to be implemented to receive events from button » actionPerformed(ActionEvent e) is called when the event occurs and the event object is passed as an argument https://www.facebook.com/Oxus20 6
  • 7. Java Events » Events are objects ˃ Objects that represent user initiated actions ˃ Examples: + button clicked -> ActionEvent + mouse dragged -> MouseEvent + Enter key pressed -> KeyEvent ˃ EventObject; root event class for all event objects ˃ AWTEvent; root event class for all AWT events ˃ Package java.awt.event + Provides interfaces and classes for dealing with different types of events fired by AWT components. 7 https://www.facebook.com/Oxus20
  • 9. Event Handling » Programmer choice to decide how to handle the generated event ˃ Ignore the Event ˃ Have the Event handled by the component where the event was generated (Self Contained Event handling) ˃ Delegate event handling to some other object called Listeners (Event Delegation) 9 https://www.facebook.com/Oxus20
  • 10. Event Delegation » Some time component on which event was generated is not best suited to handle its own event » The process of assigning an object to handle a component’s events is called delegation. » The event handling objects are called Listeners 10 https://www.facebook.com/Oxus20
  • 11. Key Methods of Event » Object getSource() In ObjectEvent, return the component in which event took place. » int getID() In AwtEvent, return int that describes the nature of the event e.g. on MouseEvent it will give MOUSE_PRESSED, MOUSE_DRAGGED. 11 https://www.facebook.com/Oxus20
  • 12. Event Listeners » Interfaces to support dispatching of events » Each Event class has a corresponding Listener interface » Multiple listeners for the same event type » Each interface will have one or more method corresponding to types of events » ActionEvent -> ActionListener » MouseEvent -> MouseListener and MouseMotionListener https://www.facebook.com/Oxus20 12
  • 13. Registering Listeners » Listeners register themselves with component ˃ public void addXXXListener(XXXListener) ˃ addActionListener, addItemListener, etc. » Multiple listeners can be registered for the same event on a component ˃ One event can trigger numerous responses ˃ Events are broadcast to all listeners 13 https://www.facebook.com/Oxus20
  • 14. Wiring a Listener » Define a class to implement the Listener Interface Public class Applet extends Applet implements ActionListener public class MyClass implements ActionListener { » Add the implementation of the Interface … public void actionPerformed(ActionEvent e) { // here’s where I do stuff when the action happens … » Add class as a Listener to Component … Button ok = new Button(“OK”) ok.addActionListener(this); 14 https://www.facebook.com/Oxus20
  • 15. ActionListener Example import import import import import javax.swing.*; java.awt.FlowLayout; java.awt.Dimension; java.awt.event.ActionListener; java.awt.event.ActionEvent; public class ActionListenerTest extends JFrame implements ActionListener { JTextArea topTextArea; JTextArea bottomTextArea; JButton button1, button2; final static String newline = "n"; 15 https://www.facebook.com/Oxus20
  • 16. ActionListener Example (cont.) public ActionListenerTest() { setLayout(new FlowLayout()); topTextArea = new JTextArea(); topTextArea.setEditable(false); JScrollPane topScrollPane = new JScrollPane(topTextArea); topScrollPane.setPreferredSize(new Dimension(200, 75)); add(topScrollPane); bottomTextArea = new JTextArea(); bottomTextArea.setEditable(false); JScrollPane bottomScrollPane = new JScrollPane(bottomTextArea); bottomScrollPane.setPreferredSize(new Dimension(200, 75)); 16 https://www.facebook.com/Oxus20
  • 17. ActionListener Example (cont.) add(bottomScrollPane); button1 = new JButton("top Text area"); add(button1); button2 = new JButton("down text area"); add(button2); button1.addActionListener(this); button2.addActionListener(this); setSize(300, 222); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } 17 https://www.facebook.com/Oxus20
  • 18. ActionListener Example(cont.) public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) { topTextArea.append(e.getActionCommand() + newline); } if (e.getSource() == button2) { bottomTextArea.append(e.getActionCommand() + newline); } } public static void main(String[] args) { new ActionListenerTest(); } } 18 https://www.facebook.com/Oxus20