SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Introduction JFrame JPanel Event Listeners Summary References
THE AWT AND SWING
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 22, 2015
Introduction JFrame JPanel Event Listeners Summary References
OUTLINE I
1 INTRODUCTION
2 JFRAME
3 JPANEL
4 EVENT LISTENERS
5 SUMMARY
6 REFERENCES
Introduction JFrame JPanel Event Listeners Summary References
INTRODUCTION
In this chapter we will examine the Java AWT library for
graphical user interfaces, noting in particular how it uses
the framework concepts developed in the last chapter.
The class Frame for the main windows.
The Layout Manager classes.
Event Listeners.
User Interface Components.
Introduction JFrame JPanel Event Listeners Summary References
TENSION BETWEEN REUSE AND SPECIALIZATION
A truly general purpose tool cannot contain features
specific to any one application.
Solving most problems requires application specific
features.
How do you bridge the gap between general purpose and
application independent tools and an application that will
solve a real problem?
Introduction JFrame JPanel Event Listeners Summary References
CLASSES FRAME AND JFRAME
The place to begin is the class Frame (in AWT) or JFrame
(in Swing).
These give the overall window for the application, and hold
features such as the title bar and menu bar.
Many foundation methods that provide a huge amount of
standard behavior.
A few methods that are sometimes overriden, such as
paint.
This is a straightforward implementation of the framework
ideas discussed in the previous chapter.
he Swing library was introduced in Java 1.3, and has
slowly replaced the older AWT library.
Introduction JFrame JPanel Event Listeners Summary References
PANEL AND JPANEL
The window is combined with an instance of class Panel
(JPanel in Swing).
This class is the screen on to which drawing operations are
directed.
JPanel can include components (buttons and the like) and
can refine the method paint.
(The overridden method must invoke the parent method, in
order to render features of the window not included in the
user constructed portions).
EXAMPLE
class MyPanel extends JPanel {
. . .
public void paint ( Graphics g ) {
super . paint ( g ) ;
. . .
}
}
Introduction JFrame JPanel Event Listeners Summary References
LAYOUT MANAGER
Here is a problem.
The algorithm used to lay out various graphical elements
(buttons and the like) can only be determined by the end
programmer.
But writing layout algorithms is complex, probably not
something most programmers can do.
So, how to provide the necessarily flexibility within a simple
framework?
Solution – provide a variety of different layout managers
(border layout, grid layout, so on), allow the end
programmer to select one of the many choices, and give it
back to the framework.
Introduction JFrame JPanel Event Listeners Summary References
SHOWING THE USE OF A LAYOUT MANAGER
EXAMPLE
class MyPanel extends JPanel {
public MyPanel ( ) {
. . .
setLayoutManger (new BorderLayout ( ) ) ;
}
}
Both JPanel and BorderLayout are part of the framework,
not written by the end programmer.
The end programmer selects the appropriate type of layout
manager.
In this fashion the framework provides a flexible solution,
without making the frame class overly large.
(As it would be if it needed to hold all layout managers).
Introduction JFrame JPanel Event Listeners Summary References
EVENT LISTENERS
Event listeners are another good example of the use of
inheritance and overriding in the Java AWT framework.
Every component (window, button, scroll bar) maintains a
list of “listeners”; objects that are interested in being
notified when an event has taken place.
Each type of event is defined by an interface, such as the
following:
EXAMPLE
public interface MouseListener extends EventListener {
public void mouseClicked ( MouseEvent e ) ;
public void mouseEntered ( MouseEvent e ) ;
public void mouseExited ( MouseEvent e ) ;
public void mousePressed ( MouseEvent e ) ;
public void mouseReleased ( MouseEvent e ) ;
}
Introduction JFrame JPanel Event Listeners Summary References
ADAPTERS REDUCE CODE SIZE
EXAMPLE
public class MouseAdapter implements MouseListener {
public void mouseClicked ( MouseEvent e ) { }
public void mouseEntered ( MouseEvent e ) { }
public void mouseExited ( MouseEvent e ) { }
public void mousePressed ( MouseEvent e ) { }
public void mouseReleased ( MouseEvent e ) { }
}
Introduction JFrame JPanel Event Listeners Summary References
CREATING A LISTENER
The programmer then creates a new class that subclasses the
adapter and overrides the methods of interest, then registers an
instance of the class with the component.
EXAMPLE
class MyWindow extends JFrame {
public MyWindow ( ) {
. . .
addMouseListener (new MouseKeeper ( ) ) ;
}
. . .
private class MouseKeeper extends MouseAdapter {
public void mousePressed ( MouseEvent e ) {
. . .
}
}
}
Inner classes are particularly usefor for this idiom.
Introduction JFrame JPanel Event Listeners Summary References
BUTTONS, SLIDERS, TEXT BOXES, AND SO ON
Graphical components (Buttons, Sliders, Text Boxes, and
many many more) are provided in the standard library.
The user places an instance of a class into the window.
Most use the listener event model to notify when they are
changed.
EXAMPLE
Button butn = new Button ( " do i t ! " ) ;
add ( " North " , butn ) ; / / place at top of screen
butn . addActionListener (new doIt ( ) ) ; / / add l i s t e n e r
. .
private class doIt implements ActionListener {
public void actionPerformed ( ActionEvent e ) {
/ / what ever do i t does
. . .
}
}
Introduction JFrame JPanel Event Listeners Summary References
COMBINING COMPONENT AND LISTENER IN ONE CLASS
Sometimes, a better encapsulation results from combining
inheritance from the component class with implementation
of the listener.
The component then becomes its own listener.
EXAMPLE
class ColorButton extends Button implements ActionListener {
private Color ourColor ;
public ColorButton ( Color c , String name) {
super (name ) ; / / create the button
ourColor = c ; / / save the color value
addActionListener ( this ) ; / / add ourselves as l i s t e n e r
}
public void actionPerformed ( ActionEvent e ) {
/ / set color f o r middle panel
setFromColor ( ourColor ) ;
}
}
Introduction JFrame JPanel Event Listeners Summary References
SUMMARY
The AWT illustrates the application of the framework
concept at many different levels.
In this chapter we have examined.
The window class, inheriting from Frame.
The panel class, inheriting from JPanel.
The layout manager, the user selecting one of several
alternatives.
The event model, using the concept of listeners.
The standard components.
Introduction JFrame JPanel Event Listeners Summary References
REFERENCES
Images and content for developing these slides have been
taken from the follwoing book with the permission of the
author.
An Introduction to Object Oriented Programming, Timothy
A. Budd.
This presentation is developed with Beamer:
Darmstadt, monarca.

Contenu connexe

Tendances

Tendances (20)

Complete java swing
Complete java swingComplete java swing
Complete java swing
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
Gui
GuiGui
Gui
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and Components
 
Java swing
Java swingJava swing
Java swing
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
Java swing
Java swingJava swing
Java swing
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 
swingbasics
swingbasicsswingbasics
swingbasics
 
GUI programming
GUI programmingGUI programming
GUI programming
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Java Swing JFC
Java Swing JFCJava Swing JFC
Java Swing JFC
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
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
 
25 awt
25 awt25 awt
25 awt
 
Chapter 1 swings
Chapter 1 swingsChapter 1 swings
Chapter 1 swings
 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Graphical User Interface (Gui)
Graphical User Interface (Gui)Graphical User Interface (Gui)
Graphical User Interface (Gui)
 

En vedette

4 gu is-andinheritance
4 gu is-andinheritance4 gu is-andinheritance
4 gu is-andinheritance
notshoaib
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
Palak Sanghani
 

En vedette (11)

Oop lecture9 10
Oop lecture9 10Oop lecture9 10
Oop lecture9 10
 
4 gu is-andinheritance
4 gu is-andinheritance4 gu is-andinheritance
4 gu is-andinheritance
 
Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]Lec 11 12_sept [compatibility mode]
Lec 11 12_sept [compatibility mode]
 
Centipetal force[1]
Centipetal force[1]Centipetal force[1]
Centipetal force[1]
 
UCM 6
UCM 6UCM 6
UCM 6
 
Rock Candy | Beauty & The Beast
Rock Candy | Beauty & The BeastRock Candy | Beauty & The Beast
Rock Candy | Beauty & The Beast
 
10.3 Android Video
10.3 Android Video10.3 Android Video
10.3 Android Video
 
Swing
SwingSwing
Swing
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
 
Java OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - SwingJava OOP Programming language (Part 7) - Swing
Java OOP Programming language (Part 7) - Swing
 
Centripetal Force
Centripetal ForceCentripetal Force
Centripetal Force
 

Similaire à The AWT and Swing

Event handling63
Event handling63Event handling63
Event handling63
myrajendra
 

Similaire à The AWT and Swing (20)

Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
Event handling63
Event handling63Event handling63
Event handling63
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
 
oops with java modules iii & iv.pptx
oops with java modules iii & iv.pptxoops with java modules iii & iv.pptx
oops with java modules iii & iv.pptx
 
Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
 
myslide1
myslide1myslide1
myslide1
 
myslide6
myslide6myslide6
myslide6
 
NewSeriesSlideShare
NewSeriesSlideShareNewSeriesSlideShare
NewSeriesSlideShare
 
Applet progming
Applet progmingApplet progming
Applet progming
 
Whoops! Where did my architecture go?
Whoops! Where did my architecture go?Whoops! Where did my architecture go?
Whoops! Where did my architecture go?
 
Applet in java new
Applet in java newApplet in java new
Applet in java new
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
 
L11cs2110sp13
L11cs2110sp13L11cs2110sp13
L11cs2110sp13
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
AdvancedJava.pptx
AdvancedJava.pptxAdvancedJava.pptx
AdvancedJava.pptx
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Rcp by example
Rcp by exampleRcp by example
Rcp by example
 
Session2-J2ME development-environment
Session2-J2ME development-environmentSession2-J2ME development-environment
Session2-J2ME development-environment
 

Plus de adil raja

Plus de adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 

Dernier

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Dernier (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

The AWT and Swing

  • 1. Introduction JFrame JPanel Event Listeners Summary References THE AWT AND SWING Muhammad Adil Raja Roaming Researchers, Inc. cbna April 22, 2015
  • 2. Introduction JFrame JPanel Event Listeners Summary References OUTLINE I 1 INTRODUCTION 2 JFRAME 3 JPANEL 4 EVENT LISTENERS 5 SUMMARY 6 REFERENCES
  • 3. Introduction JFrame JPanel Event Listeners Summary References INTRODUCTION In this chapter we will examine the Java AWT library for graphical user interfaces, noting in particular how it uses the framework concepts developed in the last chapter. The class Frame for the main windows. The Layout Manager classes. Event Listeners. User Interface Components.
  • 4. Introduction JFrame JPanel Event Listeners Summary References TENSION BETWEEN REUSE AND SPECIALIZATION A truly general purpose tool cannot contain features specific to any one application. Solving most problems requires application specific features. How do you bridge the gap between general purpose and application independent tools and an application that will solve a real problem?
  • 5. Introduction JFrame JPanel Event Listeners Summary References CLASSES FRAME AND JFRAME The place to begin is the class Frame (in AWT) or JFrame (in Swing). These give the overall window for the application, and hold features such as the title bar and menu bar. Many foundation methods that provide a huge amount of standard behavior. A few methods that are sometimes overriden, such as paint. This is a straightforward implementation of the framework ideas discussed in the previous chapter. he Swing library was introduced in Java 1.3, and has slowly replaced the older AWT library.
  • 6. Introduction JFrame JPanel Event Listeners Summary References PANEL AND JPANEL The window is combined with an instance of class Panel (JPanel in Swing). This class is the screen on to which drawing operations are directed. JPanel can include components (buttons and the like) and can refine the method paint. (The overridden method must invoke the parent method, in order to render features of the window not included in the user constructed portions). EXAMPLE class MyPanel extends JPanel { . . . public void paint ( Graphics g ) { super . paint ( g ) ; . . . } }
  • 7. Introduction JFrame JPanel Event Listeners Summary References LAYOUT MANAGER Here is a problem. The algorithm used to lay out various graphical elements (buttons and the like) can only be determined by the end programmer. But writing layout algorithms is complex, probably not something most programmers can do. So, how to provide the necessarily flexibility within a simple framework? Solution – provide a variety of different layout managers (border layout, grid layout, so on), allow the end programmer to select one of the many choices, and give it back to the framework.
  • 8. Introduction JFrame JPanel Event Listeners Summary References SHOWING THE USE OF A LAYOUT MANAGER EXAMPLE class MyPanel extends JPanel { public MyPanel ( ) { . . . setLayoutManger (new BorderLayout ( ) ) ; } } Both JPanel and BorderLayout are part of the framework, not written by the end programmer. The end programmer selects the appropriate type of layout manager. In this fashion the framework provides a flexible solution, without making the frame class overly large. (As it would be if it needed to hold all layout managers).
  • 9. Introduction JFrame JPanel Event Listeners Summary References EVENT LISTENERS Event listeners are another good example of the use of inheritance and overriding in the Java AWT framework. Every component (window, button, scroll bar) maintains a list of “listeners”; objects that are interested in being notified when an event has taken place. Each type of event is defined by an interface, such as the following: EXAMPLE public interface MouseListener extends EventListener { public void mouseClicked ( MouseEvent e ) ; public void mouseEntered ( MouseEvent e ) ; public void mouseExited ( MouseEvent e ) ; public void mousePressed ( MouseEvent e ) ; public void mouseReleased ( MouseEvent e ) ; }
  • 10. Introduction JFrame JPanel Event Listeners Summary References ADAPTERS REDUCE CODE SIZE EXAMPLE public class MouseAdapter implements MouseListener { public void mouseClicked ( MouseEvent e ) { } public void mouseEntered ( MouseEvent e ) { } public void mouseExited ( MouseEvent e ) { } public void mousePressed ( MouseEvent e ) { } public void mouseReleased ( MouseEvent e ) { } }
  • 11. Introduction JFrame JPanel Event Listeners Summary References CREATING A LISTENER The programmer then creates a new class that subclasses the adapter and overrides the methods of interest, then registers an instance of the class with the component. EXAMPLE class MyWindow extends JFrame { public MyWindow ( ) { . . . addMouseListener (new MouseKeeper ( ) ) ; } . . . private class MouseKeeper extends MouseAdapter { public void mousePressed ( MouseEvent e ) { . . . } } } Inner classes are particularly usefor for this idiom.
  • 12. Introduction JFrame JPanel Event Listeners Summary References BUTTONS, SLIDERS, TEXT BOXES, AND SO ON Graphical components (Buttons, Sliders, Text Boxes, and many many more) are provided in the standard library. The user places an instance of a class into the window. Most use the listener event model to notify when they are changed. EXAMPLE Button butn = new Button ( " do i t ! " ) ; add ( " North " , butn ) ; / / place at top of screen butn . addActionListener (new doIt ( ) ) ; / / add l i s t e n e r . . private class doIt implements ActionListener { public void actionPerformed ( ActionEvent e ) { / / what ever do i t does . . . } }
  • 13. Introduction JFrame JPanel Event Listeners Summary References COMBINING COMPONENT AND LISTENER IN ONE CLASS Sometimes, a better encapsulation results from combining inheritance from the component class with implementation of the listener. The component then becomes its own listener. EXAMPLE class ColorButton extends Button implements ActionListener { private Color ourColor ; public ColorButton ( Color c , String name) { super (name ) ; / / create the button ourColor = c ; / / save the color value addActionListener ( this ) ; / / add ourselves as l i s t e n e r } public void actionPerformed ( ActionEvent e ) { / / set color f o r middle panel setFromColor ( ourColor ) ; } }
  • 14. Introduction JFrame JPanel Event Listeners Summary References SUMMARY The AWT illustrates the application of the framework concept at many different levels. In this chapter we have examined. The window class, inheriting from Frame. The panel class, inheriting from JPanel. The layout manager, the user selecting one of several alternatives. The event model, using the concept of listeners. The standard components.
  • 15. Introduction JFrame JPanel Event Listeners Summary References REFERENCES Images and content for developing these slides have been taken from the follwoing book with the permission of the author. An Introduction to Object Oriented Programming, Timothy A. Budd. This presentation is developed with Beamer: Darmstadt, monarca.