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.