Outline
Other Material
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.
This is a straightforward implementation of the framework ideas discussed in the previous chapter.
The Swing library was introduced in Java 1.3, and has slowly replaced the older AWT library.
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).
class MyPanel extends JPanel { ... public void paint (Graphics g) { super.paint(g); ... } }
Here is a problem.
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).
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:
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); }
More often than not, programmers are interested in only one or two events, not all the possible events defined by the interface.
An adapter provides empty implementations, allowing the programmer to redefine only the events of interest.
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) { } }
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.
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.
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.
Button butn = new Button ("do it!"); add("North", butn); // place at top of screen butn.addActionListener (new doIt()); // add listener .. private class doIt implements ActionListener { public void actionPerformed (ActionEvent e) { // what ever do it does ... } }
The listener model avoids the need for inheritance from the component 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.
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 listener } public void actionPerformed (ActionEvent e) { // set color for middle panel setFromColor (ourColor); } }
The AWT illustrates the application of the framework concept at many different levels. In this chapter we have examined